help using for while loop

having trouble using for while loop

this is what im supposed to do:

Write a program that will read an unspecified number of positive numbers from the keyboard and determine the sum and the average of these numbers. A negative number should be used to terminate the input, i.e. when the program encounters a negative number, it should stop prompting and reading data and at that point should output the results. Note that the negative number that terminates the looping should not be added to the sum and certainly should not be counted as one of the valid numbers.

A sample program run would look like this :

Enter a number that is positive or zero 3

Enter a number that is positive or zero 5

Enter a number that is positive or zero 6

Enter a number that is positive or zero 4

Enter a number that is positive or zero -1

You have entered 4 numbers

Total of the numbers you entered is 18

Average of the numbers is 4.5

The program should use a For WHILE loop to accomplish it's mission.

here is my code and i dont think its anywhere close to right

Code java:

import java.util.Scanner ;
public class Assign7_1_Roberts{
public static void main (String [] args){
//Create Scanner
Scanner input = new Scanner(System.in);
i = initialValue;
int number = 0, i = 0; sum = 0, numamount = 0; // You can change the variable names.
for(i = 0; i < 100; i++)
while(i < endValue1)
{ // Begin while statement
System.out.println("Enter a number that is positive or zero: "); // Get the next number
String userInput = input.nextLine();
sum = sum+number; // Get the sum so far.
numamount++; // Get the number amount so far.
} // End while statement.
double average = sum/numamount; //Get the average.
System.out.println("You have entered " + numamount + " numbers"); // Output amount of numbers
System.out.println("Total of the numbers you entered is " + sum); // Output sum of numbers
System.out.println("Average of the numbers is " + average); // Output average of numbers.
}
}

Re: help using for while loop

Quote:

Originally Posted by

**robertsbd**
having trouble using for while loop

this is what im supposed to do:

Write a program that will read an unspecified number of positive numbers from the keyboard and determine the sum and the average of these numbers. A negative number should be used to terminate the input, i.e. when the program encounters a negative number, it should stop prompting and reading data and at that point should output the results. Note that the negative number that terminates the looping should not be added to the sum and certainly should not be counted as one of the valid numbers.

A sample program run would look like this :

Enter a number that is positive or zero 3

Enter a number that is positive or zero 5

Enter a number that is positive or zero 6

Enter a number that is positive or zero 4

Enter a number that is positive or zero -1

You have entered 4 numbers

Total of the numbers you entered is 18

Average of the numbers is 4.5

The program should use a For WHILE loop to accomplish it's mission.

here is my code and i dont think its anywhere close to right

Code java:

import java.util.Scanner ;
public class Assign7_1_Roberts{
public static void main (String [] args){
//Create Scanner
Scanner input = new Scanner(System.in);
i = initialValue;
int number = 0, i = 0; sum = 0, numamount = 0; // You can change the variable names.
for(i = 0; i < 100; i++)
while(i < endValue1)
{ // Begin while statement
System.out.println("Enter a number that is positive or zero: "); // Get the next number
String userInput = input.nextLine();
sum = sum+number; // Get the sum so far.
numamount++; // Get the number amount so far.
} // End while statement.
double average = sum/numamount; //Get the average.
System.out.println("You have entered " + numamount + " numbers"); // Output amount of numbers
System.out.println("Total of the numbers you entered is " + sum); // Output sum of numbers
System.out.println("Average of the numbers is " + average); // Output average of numbers.
}
}

1.) You never have an ending bracket for the for loop.

2.) Where is endValue1 defined?

3.) Either have each variable defined on its own line if you're going to initialize them right away or

have them on the same line, but don't initialize them till later, but not too late that your for and while loops don't know what they are, and do so one per line.

4.) You're doing nothing with the String userInput.

5.) It appears that number will always be 0 inside your while loop. Is that what you want it to do?

6.) since sum is set to 0 and so is number, it'll always be 0 for each iteration of the while loop.

in other words

0 = 0 + 0;

0 = 0 + 0;

number is never changing its value and so neither is sum.

Maybe the number the user enters is supposed to be what you're adding.

if so, it'd be input.nextInt().

Re: help using for while loop

so it seems i gotta lot of work to do..im really new to java so im still tryin to figure out what all lingo used in java means

Re: help using for while loop

i also have to do the exact same thing but using the do-while loop

Re: help using for while loop

im working on the do-while loop assignment and the output "enter a number that is positive or zero" doesnt end

heres my code

Code java:

import java.util.Scanner ;
public class Assign7_2_Roberts{
public static void main (String [] args){
int data;
int sum = 0, numamount = 0; // You can change the variable names.
//Create Scanner
Scanner input = new Scanner(System.in);
do { // Begin do-while statement
System.out.println("Enter a number that is positive or zero: "); // Get the next number
data = input.nextInt();
sum += data; // Get the sum so far.
} while (data >= 0); // End while statement.
double average = sum/numamount; //Get the average.
System.out.println("You have entered " + numamount + " numbers"); // Output amount of numbers
System.out.println("Total of the numbers you entered is " + sum); // Output sum of numbers
System.out.println("Average of the numbers is " + average); // Output average of numbers.
}
}

Re: help using for while loop

In your do while loop, you are continuing as long as data is not equal to zero, but in the description of your assignment, it says to read numbers, until the user enters a negative number.

Re: help using for while loop

ok i changed from !=0 to >=0 but when i enter a negative number i get this

Exception in thread "main" java.lang.ArithmeticException: / by zero

at Assign7_2_Roberts.main(Assign7_2_Roberts.java:26)

Re: help using for while loop

Re: help using for while loop

You are dividing by numaount when calculating average. You initialize numamount to 0, then it never changes. You can't divide by 0