Results 1 to 7 of 7
  1. #1
    pytho is offline Member
    Join Date
    Jun 2011
    Location
    Indiana
    Posts
    6
    Rep Power
    0

    Arrow 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!

    Java 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();
          }
        }
      }

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Add an interger that will hold the sum of numbers entered. Then in your while loop add sumOfNumber + number

  3. #3
    pytho is offline Member
    Join Date
    Jun 2011
    Location
    Indiana
    Posts
    6
    Rep Power
    0

    Default

    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

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

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

  5. #5
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    sumOfNumber should start at zero.

    Then inside your loop put an operation that adds the number to sumOfNumber.

  6. #6
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Quote Originally Posted by sunde887 View Post
    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

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I know, just trying to push him in the right direction.

    Perhaps some pseudo code shall help
    Java 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

Similar Threads

  1. Need HW help on java homework
    By sourlemons in forum New To Java
    Replies: 3
    Last Post: 10-16-2010, 05:44 AM
  2. Need help with my java homework.
    By nightrise420 in forum New To Java
    Replies: 6
    Last Post: 09-24-2010, 07:23 PM
  3. java homework help
    By jenniferrlie in forum New To Java
    Replies: 5
    Last Post: 09-22-2009, 08:12 PM
  4. Java homework please
    By Indulgence in forum New To Java
    Replies: 1
    Last Post: 11-03-2008, 02:48 AM
  5. LF: Homework help with Java
    By excurssion in forum New To Java
    Replies: 2
    Last Post: 10-17-2008, 06:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •