help using loop

• October 20th, 2010, 01:14 PM
robertsbd
help using loop
im doing an assignment for java intro class and am getting the results i need just dont know if this is layed out right or not

here is what were 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 WHILE loop to accomplish it's mission.

and here is my code

Code java:

` `
Code java:

```    import java.util.Scanner ;   public class Assign6_Roberts{   public static void main (String [] args){ //Create Scanner Scanner input = new Scanner(System.in);     System.out.println("Enter a number that is positive or zero: "); int num = input.nextInt();   System.out.println("Enter a number that is positive or zero: "); int num1 = input.nextInt();   System.out.println("Enter a number that is positive or zero: "); int num2 = input.nextInt();   System.out.println("Enter a number that is positive or zero: "); int num3 = input.nextInt(); int sum = 0;   System.out.println("You have entered 4 numbers ");   System.out.println("Total of numbers is " + (num + num1 + num2 + num3)); System.out.println("Average of numbers is " + (num + num1 + num2 + num3) / 4); while (num <0){ sum+= num; } while (num <0){ sum/= num;     }     } } [B][/B]```
• October 20th, 2010, 01:35 PM
Tjstretch
Re: help using loop
Ok heres a hint.

For your while loop, your going to want it to look something around

Code java:

```int number, sum, numamount; // You can change the variable names. while(input>=0) { // Begin while statement System.out.println("Enter a number that is positive or zero: "); // Get the next number number = input.nextInt();   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.```
• October 20th, 2010, 01:42 PM
robertsbd
Re: help using loop
im gettin an error at
while (input >=0)..is something missing?
• October 20th, 2010, 01:49 PM
Tjstretch
Re: help using loop
Oh I've seen that error. Do this instead for the while loop
Code java:

`while(input>-1)`
• October 20th, 2010, 01:54 PM
robertsbd
Re: help using loop
it did the same thing..it says operator > is undefined for the argument type Scanner.int
• October 20th, 2010, 01:56 PM
Tjstretch
Re: help using loop
Oh duh, I fail fix it to look like
Code java:

`while(number>-1)`
• October 20th, 2010, 02:07 PM
robertsbd
Re: help using loop
did the same thing again
• October 20th, 2010, 02:08 PM
Tjstretch
Re: help using loop
Ok if you have yahoo message me, otherwise:
Oh think I found the error.
before your while statement, initialize the number by :

Code java:

` int number = 0, sum = 0, numamount = 0; // You can change the variable names.`
• October 20th, 2010, 07:56 PM
Darryl.Burke
Re: help using loop
Quote:

Originally Posted by Tjstretch
Ok if you have yahoo message me

That goes against the purpose of a forum. What's wrong with discussing the problem here?

You wouldn't be collecting email addresses for spamming, now... or would you?

db
• October 20th, 2010, 08:14 PM
robertsbd
Re: help using loop
it fixed that problem but when i run it it constantly keeps asking me to put in a number when i only want to enter about 4 or 5 numbers and and then calculate the sum and average of those #'s

btw this is now what it looks like

Code java:

```import java.util.Scanner ;   public class Assign6_Roberts{   public static void main (String [] args){ //Create Scanner Scanner input = new Scanner(System.in);     int number = 0, sum = 0, numamount = 0; // You can change the variable names. while(number >-1) { // Begin while statement System.out.println("Enter a number that is positive or zero: "); // Get the next number number = input.nextInt();   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. } }```
• October 20th, 2010, 08:54 PM
robertsbd
Re: help using loop
bump......
• October 22nd, 2010, 10:11 AM
mike.s
Re: help using loop
you've posted this twice. i helped you with it in your other thread. you should check it out :)