new to java: homework question

• 06-24-2011, 05:17 AM
pytho
new to java: homework question
hi everyone, i am new to programming and am having a really hard time on my homework assignment. we wrote a program in class that asks the user to input the number of *'s they want, and that number of *'s is printed. this is in a loop, and the question is continually asked until the user enters 'bye' .

on the hw, i was given a similar question: this program is supposed to read numbers from the keyboard, one at a time. every time a number is read, it is added to the sum of all numbers entered, which is then printed. the average of all the #'s entered should be calculated & printed. this also ends when the user enters 'bye' .

if anyone can point me in the right direction it would be really really helpful!

Code:

```import java.util.*; class SumofNums {   public static void main(String[] args) {     Scanner zip = new Scanner(System.in);     int number;     String input;     while(true) {       System.out.print("Enter a number: ");       input = zip.nextLine();       if (input.equalsIgnoreCase ("bye") ) {         break;       } else {         number = Integer.parseInt(input);  //this is where i started having trouble.         while (number > 0) {                      //i'm not sure what to put in the while loop           System.out.print(number);  //or how to keep track of the numbers entered.           number = number - 1;                   }         System.out.println();       }     }   }```
• 06-24-2011, 05:30 AM
mine0926
Add an interger that will hold the sum of numbers entered. Then in your while loop add sumOfNumber + number
• 06-24-2011, 05:44 AM
pytho
maybe i am overthinking it, but how can the variable hold the sum of all numbers entered?
i tried to assign it as: int sumOfNumber = zip.nextInt(); but that does not seem to do the trick
• 06-24-2011, 06:19 AM
sunde887
What happens if you reassign the variable to itself plus input? You will also need to increment a counter.
• 06-24-2011, 06:25 AM
mine0926
sumOfNumber should start at zero.

Then inside your loop put an operation that adds the number to sumOfNumber.
• 06-24-2011, 06:26 AM
mine0926
Quote:

Originally Posted by sunde887
What happens if you reassign the variable to itself plus input? You will also need to increment a counter.

I think OP is just keep on assigning sumOfNumber but he is not adding the number into it
• 06-24-2011, 06:31 AM
sunde887
I know, just trying to push him in the right direction.

Perhaps some pseudo code shall help
Code:

```create int variable for sum and count loop   get input   If input is "bye", end loop   parse input and add to sum   print current value   increment count end loop compute average and print it```