1. Senior Member
Join Date
Feb 2011
Posts
255
Rep Power
6

## 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;

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?
they can be divided by 2 later
}

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 02:15 AM.

2. 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. 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. Senior Member
Join Date
Feb 2011
Posts
255
Rep Power
6
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 04:05 PM.

5. 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
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. Originally Posted by sunde887
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

7. Senior Member
Join Date
Feb 2011
Posts
255
Rep Power
6
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 03:54 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
•