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 12 of 12

Thread: help using loop

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

    Default 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

     
     
     
    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]
    Last edited by robertsbd; October 20th, 2010 at 01:16 PM.


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: help using loop

    Ok heres a hint.

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

    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.

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

    Default Re: help using loop

    im gettin an error at
    while (input >=0)..is something missing?

  4. #4
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: help using loop

    Oh I've seen that error. Do this instead for the while loop
    while(input>-1)

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

    Default Re: help using loop

    it did the same thing..it says operator > is undefined for the argument type Scanner.int

  6. #6
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: help using loop

    Oh duh, I fail fix it to look like
    while(number>-1)

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

    Default Re: help using loop

    did the same thing again

  8. #8
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default 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 :

     int number = 0, sum = 0, numamount = 0; // You can change the variable names.
    Post your entire error.
    Last edited by Tjstretch; October 20th, 2010 at 02:11 PM.

  9. The Following User Says Thank You to Tjstretch For This Useful Post:

    robertsbd (October 20th, 2010)

  10. #9
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    489
    Thanks
    8
    Thanked 45 Times in 43 Posts

    Default Re: help using loop

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

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

    Default 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

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

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

    Default Re: help using loop

    bump......

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

    Default Re: help using loop

    you've posted this twice. i helped you with it in your other thread. you should check it out

Similar Threads

  1. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  2. 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
  3. loop or what
    By silverspoon34 in forum Loops & Control Statements
    Replies: 5
    Last Post: November 19th, 2009, 01:10 PM
  4. Need help with loop
    By SwEeTAcTioN in forum Loops & Control Statements
    Replies: 8
    Last Post: October 25th, 2009, 05:59 PM
  5. can any one do this in a loop? (for loop)
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: October 4th, 2009, 08:31 PM