3 Replies - 426 Views - Last Post: 04 May 2009 - 05:50 PM Rate Topic: -----

#1 Bwall  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-May 09

need help with program question

Post icon  Posted 04 May 2009 - 04:49 PM

I need to write a short program to which contains "for" loops which will demonstrate that the value of the variable isum at the termination of the each loop is 17.

it also must contain
int a[] = {2,3,5,7};
with a[0] through a[3] being a[0]=2, a[1]=3, a[2]=5, a[3]=7;

int isum = 0
for (int i = 0; i<4; ++i)
isum += a[i];

thats is everything that needs to be included, this is what I have so far but i dont think that it is correct

import java.util.Scanner;

public class Assign1
{
public static void main (Strings args[])
{
Scanner inp = New Scanner(System.in);
System.out.println("Please enter 4 values");
System.out.print ("This program will display the sum of Values entered");
int i = inp.nextInt();
int A[] = new (int[4]);
int isum;
for (int i = 0; i < 4; ++i)
isum += a[i];

System.out.println(The sum of the values entered is ) + isum;
}

}
}


If anyone could help me out ASAP it would be greatly appreciated!!!

Is This A Good Question/Topic? 0
  • +

Replies To: need help with program question

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: need help with program question

Posted 04 May 2009 - 05:02 PM

View PostBwall, on 4 May, 2009 - 03:49 PM, said:

I need to write a short program to which contains "for" loops which will demonstrate that the value of the variable isum at the termination of the each loop is 17.

it also must contain
int a[] = {2,3,5,7};
with a[0] through a[3] being a[0]=2, a[1]=3, a[2]=5, a[3]=7; Yes it is written the previous line :D

Please next time
:code:
it helps us helping you

you are not correctlly reading the 4 values

int[] a = new int[4];
System.out.println("Please enter 4 values");
System.out.print ("This program will display the sum of Values entered");
for(int i = 0; i < 4; i++) {
   a[i] = inp.nextInt();
}

//now you can add them

for(int i = 0; i < 4; i++) {
   isum = isum + a[i];
}


Actually you can have calculated isum in the first loop no need to make 2 loops

for(int i = 0; i < 4; i++) {
   a[i] = inp.nextInt();
   isum = isum + a[i];
}



Actually you don't even need an array :D
for(int i = 0; i < 4; i++) {
   isum = isum + inp.nextInt();
}



And next time:
:code:

This post has been edited by pbl: 04 May 2009 - 05:14 PM

Was This Post Helpful? 1
  • +
  • -

#3 Bwall  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-May 09

Re: need help with program question

Posted 04 May 2009 - 05:16 PM

View Postpbl, on 4 May, 2009 - 04:02 PM, said:

View PostBwall, on 4 May, 2009 - 03:49 PM, said:

I need to write a short program to which contains "for" loops which will demonstrate that the value of the variable isum at the termination of the each loop is 17.

it also must contain
int a[] = {2,3,5,7};
with a[0] through a[3] being a[0]=2, a[1]=3, a[2]=5, a[3]=7; Yes it is written the previous line :D

Please next time
:code:
it helps us helping you

you are not correctlly reading the 4 values

int[] a = new int[4];
System.out.println("Please enter 4 values");
System.out.print ("This program will display the sum of Values entered");
for(int i = 0; i < 4; i++) {
   a[i] = inp.nextInt();
}

//now you can add them

for(int i = 0; i < 4; i++) {
   isum = isum + a[i];
}


Actually you can have calculated isum in the first loop no need to make 2 loops

for(int i = 0; i < 4; i++) {
   a[i] = inp.nextInt();
   isum = isum + a[i];
}



Actually you don't even need an array :D
for(int i = 0; i < 4; i++) {
   isum = isum + inp.nextInt();
}



And next time:
:code:



Do you have any clue where I went wrong with this program??? my teacher said I am close on my assignments but not quite there.

import ASphere.MySphere;

import java.util.Scanner;
public class MySphere {
public static void main ( String [] args ) {
Scanner inp = new Scanner (System.in);
System.out.println ("Please enter the value of the radius and then the value of the diameter of the Sphere");
int a = 0
int b = 0
a = inp.nextInt ();
B = inp.nextInt ();
MySphere S1 = new MySphere (a, B);
MySphere S2 = new MySphere ();
System.out.println (" The Radius of the Sphere is " + S1.getRadius() + " And the Diameter is " + S1.getDiameter());
S2.putRadius (a);
S2.putDiameter (B);
System.out.println (" The Area of the Sphere is " S2.calcArea() + "The Volume of the Sphere is " + S2.calcVolume());
}
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: need help with program question

Posted 04 May 2009 - 05:50 PM

Hey !!! Ho !!!

How can you hope to code if you can't even use an editor ?
:code:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1