Results 1 to 7 of 7

Thread: loop help

  1. #1
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default loop help

    i am having trouble with getting the average of numbers that a user enters (will divide later once as i can get the sum of all of their numbers....). they can enter as many as they like. sorry about the brackets lining up... it was perfect in java, but when copied over, it threw everything out of line... :p here is what i have (i have questions inserted):

    Java Code:
             double input;
             double max=0;
             double min=0;
             double average = 0;
             double range = 0;
             String message = "";
    			int count = 0;
    			double numbers = 0;
    			double numbersSum = 0;
          
             do
             {
             //Prompts user for their input //this does not get repeated every time a number is entered
             Scanner in = new Scanner(System.in);
             System.out.print("What is your input? (enter -1 to terminate this program: ");
             input = in.nextDouble();
             	
       		// this gets the first number, and sets it to the min and max variables	
    			for (count=0; count < 1; count++);
    				{
    					if (count == 0)
    					{
    						max = input;
    						min = input; // can't you set input=max and min?
    					}
    				}
    				System.out.println ("numbers:" + max + min); 
    				
    			for (count=0; count < 100; count++); // how do you make the restriction unlimited?
    			{
    				numbers = (Character.getNumericValue(input.charAt(count))); //double is being 
    dereferenced... how can i fix this?  also, i don't think i'm doing this right... i'm getting the numbers in each input seperatley, right?
    				numbersSum = numbersSum + numbers; // the sum of all the numbers the user added, so 
    they can be divided by 2 later
    			}  
    			
    			System.out.print ("the numbers added up are: " + numbersSum + ".");
    				
             average = ((numbersSum)/2);
                
             range = (max-min);
             
    			// this updates the max value
             while (in.hasNextDouble())
             {
             	input = in.nextDouble();
    			
             	if (input > max)
             	{
             		max = input;
             	}
             }
    			
    			// this updates the min value
    			while (in.hasNextDouble())
             {
             	input = in.nextDouble();
              	if (input < min)
             	{
            			min = input;
             	}
             }
          	}
             
             while (input != -1);
          
             System.out.println("The average of these two inputs is: " + average + ".");
             System.out.println("The range of these two inputs is: " + range + ".");
             System.out.println(message);
    Last edited by droidus; 03-08-2011 at 01:15 AM.

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

    Default

    Don't do any math or anything to start. Start small and work your way up. There are two solid ways to implement this. One is to use an arrayList<Integer>, and the other is to ask the user how many items they want to add.

    lets go over the first method(using an array list)
    You create an array list, and then set up an infinite list and inform them of when to input to end the loop. If they enter that number/symbol/etc break out of the loop. Else you will add the input to the array list until they enter the breakable number

    the other method would be to first ask them for an array size and then initialize the array to be that size, then loop that many times and continually add the user inputs until the size is reached.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Java Code:
    for (count=0; count < 1; count++); // <-- YIKES!
    {
        if (count == 0)
        {
            max = input;
            min = input;
        }
    }
    If you think very carefully about this you will realise that the for loop and if statement are totally pointless.
    // can't you set input=max and min?
    Yes, exactly how you have done it.
    // how do you make the restriction unlimited?
    Use for loops when you have a finite number of iterations. Use while loops when you have infinite number of iterations.
    //double is being dereferenced
    Primitives do not have methods. Think about which class does have the charAt method.
    // the sum of all the numbers the user added, so they can be divided by 2
    Really? According to you the average of 5, 5, 5, 5, 5, 5 is 15 and not 5.

  4. #4
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    hm, haven't learned about arrays yet...

    and for some reason, the while statement (while (in.hasNextDouble())) throws off my loop. after entering a number, it only continues the program, but does not continue to prompt the user for their next number. is this because it could be stuck in one of the loops?
    Last edited by droidus; 03-08-2011 at 03:05 PM.

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

    Default

    don't use hasNextDouble. This is possible with out arrays.

    There are two ways to do this, the first is to ask them how many numbers they would like to use(then use either a for or while loop) and prompt them for numbers n times(where n is the first number you prompted for)

    Each time through the loop you should add the input to a sum variable.

    Java Code:
    declare sum
    declare counter
    prompt for amount of numbers
    use input in loop condition
    loop(condition)
      prompt
      get input
      add to sum
    end loop
    Just do this first.

    You can also set up an infinite loop and give the user a letter/word that breaks out of the loop. With this method you will also have to keep track of how many items they input.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by sunde887 View Post
    Don't do any math or anything to start.
    Why not? A bit of math up front never hurts. At least it avoids all those cripple programs that use loops etc. that aren't even necessary.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    here is what i have so far (by the way, i am doing the last thing you mentioned, but i don't think she wants us to have to enter how many numbers we will be entering...) my biggest issue right now, is that the min and max values do not get set properly. also, how can i count how many numbers the user enters? i tried a counter here, but it doesn't work out so well.

    Java Code:
            		double min = 0;
    		double max = 0;
    		double input = 0;
    		int count = 0;
    		double range = 0;
    		int average = 0;
    		
    		do
    		{
    		 	for (count=0; count<1; count++)
    			{
    				min = input;
    				max = input;
    			}  
    			
    			for (count = 0; count>0; count++)
    			{
    				count = count++;
    			}
    			
    			//Prompts user for their input //errors here
             Scanner in = new Scanner(System.in);
             System.out.print("What is your input? (enter -1 to terminate this program: ");
             input = in.nextDouble();
    
    			if (input<min)
    			{
    				min = input;
    			}
    			
    			if (input>max)
    			{
    				max = input;
    			}
    
    		}
    		
    		while (input != -1);
    		
    		range = max - min;
    		
    		System.out.println ("The minimum value of all the points you entered is: " + min + ".");
    		System.out.println ("The maximum value of all the points you entered is: " + max + ".");
    		System.out.println ("The range is: " + range + ".");
    		System.out.println ("The average value of all the points you entered is: " + average + ".");
    		System.out.println ("The count is: " + count + ".");
    Last edited by droidus; 03-09-2011 at 02:54 AM.

Similar Threads

  1. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM
  2. [Q] Loop issue (while loop)
    By iriscience in forum New To Java
    Replies: 9
    Last Post: 01-31-2011, 04:21 PM
  3. Convert do while loop to for loop
    By sandeeptheviper in forum New To Java
    Replies: 3
    Last Post: 01-03-2011, 12:37 PM
  4. Replies: 5
    Last Post: 04-30-2010, 05:05 PM
  5. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 08:46 PM

Posting Permissions

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