Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: can someone help me with this while loop assignment

  1. #1
    Member
    Join Date
    Oct 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default can someone help me with this while loop assignment

    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.

    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 what i got

    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.
    	}
    }


  2. #2
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: can someone help me with this while loop assignment

    You have a number of errors, but not the error you are describing. I'm not going to spoon feed you the solution, but am willing to help you get there.

    1. If the first number entered is a negative number, should the code in the while loop execute? What does your program do if the first number is negative?

    2. When you are taking the input, why are you using a println? this causes the number the user types in to be on the next line from this string: "Enter a number that is positive or zero: "

    3. If I enter any number of positive integers, and then enter a negative one to quit, should that negative number be ignored, or should it be added to sum? What does your code do? Step through it before answering this question

    4. Same as #3, but this time, when a negative number is entered should numamount be incremented? What does your code do?

    5. When calculating the average, what happens when you divide two ints? What will the output be? Will we potentially lose any information, if expecting a double?
    Last edited by DavidFongs; October 20th, 2010 at 09:56 PM.

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: can someone help me with this while loop assignment

    The reason you are entering more then 5 number is because the while loop will go until someone enters a number<1. Instead the while loop should check if numamount < 5. Then have an if statement check if the entered number<1.

     public static void main(String[] args) {
            // TODO code application logic here
             //Create Scanner
            Scanner input = new Scanner(System.in);
     
     
            int number = 0, sum = 0, numamount = 0; // You can change the variable names.
            while(numamount < 5)
            { // Begin while statement
            System.out.println("Enter  a  number  that  is  positive  or  zero: "); // Get the next number
            number = input.nextInt();
            if (number < 0){
             System.out.println("you have Entered an invalid number, please enter again: ");
             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.
        }

Similar Threads

  1. Help with assignment
    By NPVinny in forum What's Wrong With My Code?
    Replies: 12
    Last Post: July 3rd, 2010, 05:31 PM
  2. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  3. need help on an assignment :(
    By gamfreak in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 23rd, 2010, 03:20 PM
  4. Replies: 1
    Last Post: February 22nd, 2010, 07:20 AM
  5. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 04:27 PM