AoN
Internet Programmer
- Aug 1, 2012
- 114
Here's a Java question for school.
Long story short, everything in the program works perfectly, except for one "minor" detail. After the first instance of the for() loop, so for i > 1, if the first entry is not a number, a.k.a. a letter or symbol, it prints the while loop twice. After the first letter or symbol for the loop, the issue doesn't happen. This doesn't happen while i = 1, so I know I did the loops right. Here's the code:
Here's the output demonstrating the glitch:
Here's the output demonstrating a reserve of using a letter first, just to show it occurs before the second while loop:
The sadest part about all of this, is that I practically am teaching the class.
Long story short, everything in the program works perfectly, except for one "minor" detail. After the first instance of the for() loop, so for i > 1, if the first entry is not a number, a.k.a. a letter or symbol, it prints the while loop twice. After the first letter or symbol for the loop, the issue doesn't happen. This doesn't happen while i = 1, so I know I did the loops right. Here's the code:
Code:
import java.text.DecimalFormat;
import java.util.Scanner;
public class examGrades
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int min = 100;
int max = 0;
int sum = 0;
String output = null;
for(int i = 1; i <=10; i++)
{
System.out.print("Enter score number " + i + ": ");
while(!scan.hasNextInt())
{
String garbage = scan.nextLine();
System.out.print("You did not enter a number...\nEnter score number " + i + ": ");
}
int score = scan.nextInt();
while((score < 0) || (score > 100))
{
String garbage = scan.nextLine();
System.out.print("You did not enter a valid score between 0 and 100...\nEnter score number " + i + ": ");
while(!scan.hasNextInt())
{
garbage = scan.nextLine();
System.out.print("You did not enter a number...\nEnter score number " + i + ": ");
}
score = scan.nextInt();
}
if(score > max)
max = score;
if(score < min)
min = score;
if(i == 1)
output = score + ", ";
else if((i > 1) && (i < 10))
output = output + score + ", ";
else
output = output + score;
sum = sum + score;
}
System.out.println(output + "\nMin: " + min + "\nMax: " + max + "\nAverage: " + (sum / 10));
}
}
Here's the output demonstrating the glitch:
Enter score number 1: a
You did not enter a number...
Enter score number 1: -1
You did not enter a valid score between 0 and 100...
Enter score number 1: 1
Enter score number 2: a
You did not enter a number...
Enter score number 2: You did not enter a number...
Enter score number 2: a
You did not enter a number...
Enter score number 2: -1
You did not enter a valid score between 0 and 100...
Enter score number 2:
Here's the output demonstrating a reserve of using a letter first, just to show it occurs before the second while loop:
Enter score number 1: -1
You did not enter a valid score between 0 and 100...
Enter score number 1: a
You did not enter a number...
Enter score number 1: 1
Enter score number 2: -1
You did not enter a valid score between 0 and 100...
Enter score number 2: a
You did not enter a number...
Enter score number 2:
The sadest part about all of this, is that I practically am teaching the class.