# Java Coding: Assistance with a loop that calculates maximum, minimum, and average of 10 inputted numbers.

• February 26th, 2014, 08:22 PM
Knowledge_Feeds_The_Mind
Java Coding: Assistance with a loop that calculates maximum, minimum, and average of 10 inputted numbers.
Hello,
I am a newbie to Java and I am having troubles with my code. It does not calculate the maximum, minimum or average of the 10 numbers that the user is prompted to input. So I turn to the ones who know Java and programming, you. I know this may seem like a stupid question, but I hope you can give me some insight to where my problem lies. Thank you, any help is deeply appreciated.

My code:
import java.util.Scanner;

public class moretest
{
public static void main( String [ ] args )
{
int total = 0;
int number;
float average;

Scanner scan = new Scanner( System.in);

for ( int i = 0; i < 1; i++ ) //Executes 1 time
{
System.out.println("Enter ten scores between 0 and a 100");

for ( int n = 0; n < 10; n++ ) //Executes 10
{
System.out.println("Score: ");
number = scan.nextInt ( );

{
}

if ( number < minGrade )
{
}

if (number > -1 && number < 101)
{
total = total + number;
}

else{
System.out.println("Input values are not acceptable, try again."); //The message displayed when number is out of range
}
average = total/10; //to calculate the average
}
}
}
}

--- I hope I posted under the right sub-post..
• February 26th, 2014, 08:51 PM
llowe29
Re: Java Coding: Assistance with a loop that calculates maximum, minimum, and average of 10 inputted numbers.
code tags
• February 26th, 2014, 08:52 PM
dicdic
Re: Java Coding: Assistance with a loop that calculates maximum, minimum, and average of 10 inputted numbers.
[c o d e = j a v a]
[/ c o d e]
just remove the spaces I put

Actually it does computed the maxGrade, minGrade and average(if and only inputs are valid input).
The problem is you did not print them. The other problem is you store the input in max and min before checking if they are valid inputs. Try to check if they are valid before storing them in min and max.

try to print them after the outer loop but make sure to initialize your variable so you wont get compilation error regarding variables not initialize.

what if I put 123 or -13, after execution, did you really get 10 valid input?
as what I see, the program stores the value in min and max even if they are out of range (-1 > input > 100).
do some algorithm that will avoid storing the input if they are not valid.
• February 26th, 2014, 09:18 PM
Knowledge_Feeds_The_Mind
Re: Java Coding: Assistance with a loop that calculates maximum, minimum, and average of 10 inputted numbers.
Thank you for both replies.
I fixed the issue part wise, and I ran a debugger my file and there seems to be no issue. Now I am trying to run the file and it gives me an unusual output:
Enter ten scores between 0 and a 100
Score: 70
Number of values = 140
Maximum = 70.0
Minimum = 70.0
Average = 14.0

Score: 80
Number of values = 440
Maximum = 80.0
Minimum = 70.0
Average = 44.0

Score: 90
Number of values = 1060
Maximum = 90.0
Minimum = 70.0
Average = 106.0

Score: 20
Number of values = 2160
Maximum = 90.0
Minimum = 20.0
Average = 216.0

Score: 30
Number of values = 4380
Maximum = 90.0
Minimum = 20.0
Average = 438.0

Score: 40
Number of values = 8840
Maximum = 90.0
Minimum = 20.0
Average = 884.0

Score: 50
Number of values = 17780
Maximum = 90.0
Minimum = 20.0
Average = 1778.0

Score: 60
Number of values = 35680
Maximum = 90.0
Minimum = 20.0
Average = 3568.0

Score: 70
Number of values = 71500
Maximum = 90.0
Minimum = 20.0
Average = 7150.0

Score: 80
Number of values = 143160 ( is supposed to state 10)
Maximum = 90.0
Minimum = 20.0
Average = 14316.0 (should be only two decimal places)

Reformed code (Below):

[code = java]
import java.util.Scanner;

public class moretest
{
public static void main( String [ ] args )
{
int total = 0;//stores 10 numbers
int number;//stores current
float average;

Scanner scan = new Scanner( System.in);

for ( int i = 0; i < 1; i++ ) //Executes 1 time
{
System.out.println("Enter ten scores between 0 and a 100");

for ( int n = 0; n < 10; n++ ) //Executes 10
{
System.out.println("Score: ");
number = scan.nextInt ( );

}

if ( number < minGrade ){
}

if ( -1 < number && 100 > number){
total = total + number;

i++;
total += total;

}

else{
System.out.println("Input value is not acceptable, try again.");
}
if (number > 0) {
average = total/10;
System.out.println("Number of values = " + total);
System.out.println("Average = " + average);
}
}
}
}
}

[/code]

Thank you and I really appreciate the feedback!
• February 26th, 2014, 09:36 PM
dicdic
Re: Java Coding: Assistance with a loop that calculates maximum, minimum, and average of 10 inputted numbers.
Quote:

Now I am trying to run the file and it gives me an unusual output:
what do you mean by unusual output?
can you send here sample output? so we can see it?
And I just want to ask, what is the sense of your outer loop?
for ( int i = 0; i < 1; i++ ) //Executes 1 time
the condition is hard coded. so it will run only once, so what is the need of loop?

Quote:

Average = 14316.0 (should be only two decimal places)
you can use printf to make it two decimal places.
System.out.printf("%.2f", Average);
do you have any question?

and please remove spaces in tag [code = java]
• February 26th, 2014, 10:04 PM
Knowledge_Feeds_The_Mind
Re: Java Coding: Assistance with a loop that calculates maximum, minimum, and average of 10 inputted numbers.
Thank you for responding!!:)

The output is processed like this:
Number of values = 71500
Maximum = 90.0
Minimum = 20.0
Average = 7150.0

They should look like this:
Minimum score: 45
Maximum score: 94
Average score: 78.0

If I get rid of "for( int i = 0; i < 1; 1++) The program executes the function only five times....for some reason.....
Are there any other reasons why my program is not functioning correctly? Thanks again!!

Code java:

```import java.util.Scanner;   public class moretest { public static void main( String [ ] args ) { int total = 0;//stores 10 numbers int number;//stores current int minGrade = 101; int maxGrade = 0; float average;   Scanner scan = new Scanner( System.in);   for ( int i = 0; i < 1; i++ ) //Executes 1 time { System.out.println("Enter ten scores between 0 and a 100");   for ( int n = 0; n < 10; n++ ) //Executes 10 { System.out.println("Score: "); number = scan.nextInt ( );   if (number > maxGrade){ maxGrade = number; }   if ( number < minGrade ){ minGrade = number; }   if ( -1 < number && 100 > number){ total = total + number;   i++; total += total;   }   else{ System.out.println("Input value is not acceptable, try again."); } if (number > 0) { average = total/10; System.out.println("Number of values = " + total); System.out.println("Maximum = " + maxGrade); System.out.println("Minimum = " + minGrade); System.out.println("Average = " + average); } } } } }```
• February 26th, 2014, 10:27 PM
dicdic
Re: Java Coding: Assistance with a loop that calculates maximum, minimum, and average of 10 inputted numbers.
Quote:

If I get rid of "for( int i = 0; i < 1; 1++) The program executes the function only five times....for some reason.....
Are there any other reasons why my program is not functioning correctly? Thanks again!!
No, it will still execute 10 times.

Quote:

The output is processed like this:
Number of values = 71500
Maximum = 90.0
Minimum = 20.0
Average = 7150.0

They should look like this:
Minimum score: 45
Maximum score: 94
Average score: 78.0
if you want to remove Number of values = 71500 from output,
then erase this in your statement System.out.println("Number of values = " + total);

and the reason you got bugs from getting average is because of wrong arithmetic solution.
total += total;
that multiplies your total by 2. what is the reason for that?
try to print the results outside your loop.
question: do you need to print min, max and average every after input?
would it be better to do that after getting all the input?
• February 26th, 2014, 10:44 PM
Knowledge_Feeds_The_Mind
Re: Java Coding: Assistance with a loop that calculates maximum, minimum, and average of 10 inputted numbers.
Thank you again, this is very helpful for me!

I omitted total += total; What would be the correct arithmetic solution?

I do not need to print it in every input, I would prefer to do it after getting all the other input. Not fully sure on how to do though...

Code java:

```import java.util.Scanner;   public class moretest { public static void main( String [ ] args ) { int total = 0;//stores 10 numbers int number;//stores current int minGrade = 101; int maxGrade = 0; float average;   Scanner scan = new Scanner( System.in);   { System.out.println("Enter ten scores between 0 and a 100");   for ( int n = 0; n < 10; n++ ) //Executes 10 { System.out.println("Score: "); number = scan.nextInt ( );   if (number > maxGrade){ maxGrade = number; }   if ( number < minGrade ){ minGrade = number; }   if ( -1 < number && 100 > number){ total = total + number;   }   else{ System.out.println("Input value is not acceptable, try again."); } if (number > 0) { average = total/10; System.out.println("Maximum = " + maxGrade); System.out.println("Minimum = " + minGrade); System.out.println("Average = " + average); } } } } }```
• February 26th, 2014, 11:16 PM
dicdic
Re: Java Coding: Assistance with a loop that calculates maximum, minimum, and average of 10 inputted numbers.
Quote:

What would be the correct arithmetic solution?
what I meant when I said about wrong arithmetic solution is just to remove total += total
Sorry if I made you confused with that. I'm not good in English :)

just print the results after your loop
Code java:

``` if (number > 0) { average = total/10; System.out.println("Maximum = " + maxGrade); System.out.println("Minimum = " + minGrade); System.out.println("Average = " + average); }```
put that code outside your loop.
I think that code would be before second to the last curly braces.
and remove the condition if (number > 0).
but i think it will still work even you don't remove the condition.

have you tried to remove the outer loop that execute just once?
• February 26th, 2014, 11:48 PM
Knowledge_Feeds_The_Mind
Re: Java Coding: Assistance with a loop that calculates maximum, minimum, and average of 10 inputted numbers.
Thank you very much you have been extremely helpful!! I am really happy :) :) . The main problem that I have been trying to figure out has been solved and that makes me overjoyed. (I did remove the outer loop) Now all that is left is figuring out how to keep this from appearing:

Maximum = 900
Minimum = -10
Average = 35.0

and make sure that the program does not accept values less than 0 or greater than 100.

My finalized code:

Code java:

```  import java.util.Scanner;   public class moretest { public static void main( String [ ] args ) { int total = 0;//stores 10 numbers int number;//stores current int minGrade = 101; int maxGrade = 0; float average;   Scanner scan = new Scanner( System.in);   { System.out.println("Enter ten scores between 0 and a 100");   for ( int n = 0; n < 10; n++ ) //Executes 10 times   { System.out.println("Score: "); number = scan.nextInt ( );   if (number > maxGrade){ maxGrade = number; }   if ( number < minGrade ){ minGrade = number; }   if ( -1 < number && 100 > number){ total = total + number;   }   else{ System.out.println("Input value is not acceptable, try again."); }   } average = total/10; System.out.println("Maximum = " + maxGrade); System.out.println("Minimum = " + minGrade); System.out.println("Average = " + average); } } }```
• February 27th, 2014, 12:22 AM
dicdic
Re: Java Coding: Assistance with a loop that calculates maximum, minimum, and average of 10 inputted numbers.
Quote:

(I did remove the outer loop)
yes you did, but it seems that you did not remove its braces? please check although it wont a affect your program

Quote:

how to keep this from appearing:

Maximum = 900
Minimum = -10
Average = 35.0
I guess you tried to input -10 right?
and the problem is your program cant handle this.

you have this code below inside your loop:
Code java:

``` if (number > maxGrade){ maxGrade = number; }   if ( number < minGrade ){ minGrade = number; }```

it checks if the number is less than min, it also check if its greater than max, if so, you store it in min/max
and after that, you have this code below:
Code java:

``` if ( -1 < number && 100 > number){ total = total + number;   }   else{ System.out.println("Input value is not acceptable, try again."); }```
it checks if the input is valid right?

so it means that you check(if must be min or max) and store input into min and max before checking if it is a valid input right (-1 < input < 100)?
would it be better if you check if it is valid (-1 < input < 100) first before checking if it would be min or max?
and if it is not valid, then you must not store it in you minGrade or maxGrade right?

there are lots of ways to avoid executing statements inside your loop once a condition is satisfied.
the one that i suggest is continue key word in java. that will only work for loop.
if you don't know how continue works in loop, kindly see this link: Java Loops - for, while and do...while
there is a tutorial in that link I've provided

if you don't want continue, i think you can also use conditional statements if

you can do it, you are almost there
• February 27th, 2014, 12:51 AM
Knowledge_Feeds_The_Mind
Re: Java Coding: Assistance with a loop that calculates maximum, minimum, and average of 10 inputted numbers.
I fixed the brackets, thank you for pointing that out.:)
Yes it does, and it actively tells me if the value is not accepted. The problem is that the program says it is not accepted, but the value is still included in the output, even after it has been declared "non-acceptable."
(My input) : 0 < number && 100 > number, (is valid and accepted) I do not think I have a problem there...

Whenever I put continue the program states that it is "unnecessary."

Is there anything else that could be causing this program to not function optimally?

Code java:

```  import java.util.Scanner;   public class moretest { public static void main( String [ ] args ) { int total = 0;//stores 10 numbers int number;//stores current int minGrade = 10; int maxGrade = 0; float average;   Scanner scan = new Scanner( System.in);   System.out.println("Enter ten scores between 0 and a 100");   for ( int i = 0; i < 10; i++ ) //Executes 10 times { System.out.println("Score: "); number = scan.nextInt ( );   if (number > maxGrade){ maxGrade = number; }   if ( number < minGrade ){ minGrade = number; }   if ( 0 < number && 100 > number){ total = total + number;   }   else{ System.out.println("Input value is not acceptable, try again."); }   } average = total/10; System.out.println("Maximum = " + maxGrade); System.out.println("Minimum = " + minGrade); System.out.println("Average = " + average); } }```

I cannot thank you enough for your assistance. :)
• February 27th, 2014, 02:59 AM
dicdic
Re: Java Coding: Assistance with a loop that calculates maximum, minimum, and average of 10 inputted numbers.
okay,
what if you put a conditional statement right after you get the input?
after number = scan.nextInt ();? so that you can check it right after you get the user's input(that is what i'm talking about earlier)
let say you check the input if its less than 0 or greater 100.
something like if(0 < input || 100 > input)
and if so. print a validation message.
and after printing validation message, that's where you put continue keyword, continue keyword in loop will make your loop do the blocks of code from the start, it will automatically skip all lines after continue keyword. try it.
in that algorithm, once the user's input is invalid, it will automatically get another input (without doing the statements below that code)
so once its invalid, it will not recorded in min or max, it will not also computed in average.

--- Update ---

did you figured it out?
• February 27th, 2014, 12:41 PM
Knowledge_Feeds_The_Mind
Re: Java Coding: Assistance with a loop that calculates maximum, minimum, and average of 10 inputted numbers.
That worked out perfectly! I discovered two small glitches though. (Whenever I think I have finally solved this problem, a new problem appears...) This will be the last question I ask though, because everything else is minimal haha. :) Thank you for all of your assistance and your patience with a beginner with all these answers :D:D

1) The system inputs everything perfectly :), but if value is not accepted, does not prompt for a replacement value, so if score is not accepted the system moves on to the next input rather than asking to replace it. How would I change this? By adding another loop?

2) Last question how do I get my system to number the scores? Score 1:, Score 2:, Score 3:, etc.. down to 10

Code java:

```  import java.util.Scanner;   public class moretest { public static void main( String [ ] args ) { int total = 0;//stores 10 numbers int number;//stores current int minGrade = 101; int maxGrade = 0; float average;   Scanner scan = new Scanner( System.in);   System.out.println("Enter ten scores between 0 and a 100");   for ( int i = 0; i < 10; i++ ) //Executes 10 times { System.out.println("Score 1: "); number = scan.nextInt ();   if ( 100 < number || 0 > number){ System.out.println("Input value is not acceptable, try again."); continue;   }   if (number > maxGrade){ maxGrade = number; }   if ( number < minGrade ){ minGrade = number; }   if ( 0 < number && 100 > number){ total = total + number;   } } average = total/10; System.out.println("Maximum = " + maxGrade); System.out.println("Minimum = " + minGrade); System.out.println("Average = " + average); } }```
Thank you again and \m/ :D! :)
• February 27th, 2014, 06:30 PM
dicdic
Re: Java Coding: Assistance with a loop that calculates maximum, minimum, and average of 10 inputted numbers.
Quote:

1) The system inputs everything perfectly , but if value is not accepted, does not prompt for a replacement value, so if score is not accepted the system moves on to the next input rather than asking to replace it. How would I change this? By adding another loop?
look at your loop statement for ( int i = 0; i < 10; i++ )
as you noticed, it will execute 10 times right?
but actually, you can make it execute more than or less than 10 times
if you put some statement inside it.
let say in first loop, my input is invalid, then it will execute your statement continue right?
then only 9 loop is left right? because next loop, variable i = 1 and loop will execute if i < 10 right?
hmm, what if you modify your variable i whenever you get an invalid input?
try to modify it before continue statement.
that will work for sure. like subtract its current value by 1.

Quote:

2) Last question how do I get my system to number the scores? Score 1:, Score 2:, Score 3:, etc.. down to 10
try to print score concatenated by variable i.
• February 27th, 2014, 07:28 PM
Knowledge_Feeds_The_Mind
Re: Java Coding: Assistance with a loop that calculates maximum, minimum, and average of 10 inputted numbers.
Would you be able to show an example I am a little bit confused? Sorry about the inconvenience...
• February 27th, 2014, 07:52 PM
dicdic
Re: Java Coding: Assistance with a loop that calculates maximum, minimum, and average of 10 inputted numbers.
okay i'll give you example, a simple example closer to your problem.
I hope it is okay to the administrator of this forum to show some simple example.

Code java:

```import java.util.Scanner;   public class ControlLoop {   public static void main(String[] args) { Scanner scan = new Scanner(System.in); int[] numbers = new int[3]; for(int i = 0; i < 3; i++) { // executes 3 times try { System.out.println("Enter Number " + (i + 1)); numbers[i] = Integer.parseInt(scan.nextLine()); } catch (NumberFormatException e) { System.err.println("Invalid input, not a valid integer"); i--; // this will subtract i by one, something like refreshing this part of loop. } } System.out.println("\nThe inputs are"); for(int i : numbers) { // print numbers, will work only in java 6 and 7 i think System.out.println(i); } } }```

I just made the code here. not sure if it has compile error, please check if so.
I hope that will help.
try to analyze

--- Update ---

I edited the code, please check.
make sure to name your java file same as the name to that class ControlLoop
because its public

note
it can handle numbers only, so try to input letters.
that error is something like yours.
try to input invalid numbers like letters or words.. or spaces
• February 27th, 2014, 08:57 PM
Knowledge_Feeds_The_Mind
Re: Java Coding: Assistance with a loop that calculates maximum, minimum, and average of 10 inputted numbers.
It worked out, thanks :D :D
still having a problem with getting score 1:, score 2: etc, but everything else, perfect.:)
What did you mean with, "try to print score concatenated by variable i?"

Code java:

```import java.util.Scanner;   public class moretest { public static void main( String [ ] args ) { int total = 0;//stores 10 numbers int number;//stores current int minGrade = 100; int maxGrade = 0; float average;   Scanner scan = new Scanner( System.in);   System.out.println("Enter ten scores between 0 and a 100");   for ( int i = 0; i < 10; i++ ){ //Executes 10 times   System.out.println("Score 1: "); number = scan.nextInt ();   if ( 100 < number || 0 > number){ System.out.println("Input value is not acceptable, try again."); i--; continue;   }   if (number > maxGrade){ maxGrade = number; }   if ( number < minGrade ){ minGrade = number; }   if ( 0 < number && 100 > number){ total = total + number;   }   } average = total/10; System.out.println("Maximum = " + maxGrade); System.out.println("Minimum = " + minGrade); System.out.println("Average = " + average); } }```
• February 27th, 2014, 09:16 PM
dicdic
Re: Java Coding: Assistance with a loop that calculates maximum, minimum, and average of 10 inputted numbers.
Quote:

still having a problem with getting score 1:, score 2: etc, but everything else, perfect.

Quote:

"try to print score concatenated by variable i?"
I said that because of this:
Quote:

2) Last question how do I get my system to number the scores? Score 1:, Score 2:, Score 3:, etc.. down to 10
do you mean to store it in different variables? so that you can access it when you want?
like printing score 1 - score 10?
if so, you can use array.
are familiar with array in java? if not kindly visit this link:
Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
http://www.tutorialspoint.com/java/java_arrays.htm

at first I thought that you are asking about how to print Score 1, Score 2...
that is why I suggested you to print Score concatenated by your variable i
you print it like this System.out.println("Score 1: ");
I suggested you to print it like this System.out.println("Score " + i + ": ");
• February 27th, 2014, 09:26 PM
Knowledge_Feeds_The_Mind
Re: Java Coding: Assistance with a loop that calculates maximum, minimum, and average of 10 inputted numbers.
o:-) Thank you for all your help, I ran the program multiple times and it is perfect. I cannot thank you enough for your guidance and helpful nature. I hope that you have a great rest of the day!:) :)
Thank you so much!! :D

-Vex ^:)^
• February 27th, 2014, 10:11 PM
dicdic
Re: Java Coding: Assistance with a loop that calculates maximum, minimum, and average of 10 inputted numbers.