Results 1 to 7 of 7
Thread: loop help
- 03-08-2011, 01:03 AM #1
Senior Member
- Join Date
- Feb 2011
- Posts
- 231
- Rep Power
- 3
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.
- 03-08-2011, 02:53 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 03-08-2011, 02:58 AM #3
If you think very carefully about this you will realise that the for loop and if statement are totally pointless.Java Code:for (count=0; count < 1; count++); // <-- YIKES! { if (count == 0) { max = input; min = input; } }
Yes, exactly how you have done it.// can't you set input=max and min?
Use for loops when you have a finite number of iterations. Use while loops when you have infinite number of iterations.// how do you make the restriction unlimited?
Primitives do not have methods. Think about which class does have the charAt method.//double is being dereferenced
Really? According to you the average of 5, 5, 5, 5, 5, 5 is 15 and not 5.// the sum of all the numbers the user added, so they can be divided by 2
- 03-08-2011, 01:13 PM #4
Senior Member
- Join Date
- Feb 2011
- Posts
- 231
- Rep Power
- 3
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.
- 03-08-2011, 04:41 PM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
Just do this first.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
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.
- 03-08-2011, 04:48 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 03-08-2011, 04:51 PM #7
Senior Member
- Join Date
- Feb 2011
- Posts
- 231
- Rep Power
- 3
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
-
JTextField loop 2x for-loop WEIRD!
By Streetproject in forum AWT / SwingReplies: 2Last Post: 02-16-2011, 05:46 PM -
[Q] Loop issue (while loop)
By iriscience in forum New To JavaReplies: 9Last Post: 01-31-2011, 04:21 PM -
Convert do while loop to for loop
By sandeeptheviper in forum New To JavaReplies: 3Last Post: 01-03-2011, 12:37 PM -
How can I rewrite the following while loop using a for loop?
By gt11990 in forum New To JavaReplies: 5Last Post: 04-30-2010, 05:05 PM -
while-loop stopping on first loop
By davester in forum New To JavaReplies: 6Last Post: 06-26-2009, 08:46 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks