Reputation: 1
// basically i need to promp the user to enter grades and count the number of A's B's and so on. I cant figure out how to get the loop to loop once per user input. It just spams Enter an exam grade. Edit thank you all for the help I figured it out!
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int numAs = 0;
int numBs = 0;
int numCs = 0;
int numFs = 0;
int grade = 0;
while (grade >= 0 && grade <= 100)
System.out.println("Enter an exam grade");
grade = input.nextInt();
if (grade >= 90) {numAs++;
}else if (grade >=80 && grade<= 89 ) {numBs++;
}else if (grade >= 70 && grade <= 79) {numCs++;
}else if (grade <= 69) {numFs++;
}else if (grade < 0 || grade > 100) {System.out.println("Error invalid grade entered.");
}
}
}
Upvotes: 0
Views: 89
Reputation: 2776
If you formatted the code correctly, you would see that your While loop doesn't have curly braces, thus it includes only the following line in it.
Scanner input = new Scanner(System.in);
int numAs = 0;
int numBs = 0;
int numCs = 0;
int numFs = 0;
int grade = 0;
while (grade >= 0 && grade <= 100)
System.out.println("Enter an exam grade");
//WHILE ENDS HERE
grade = input.nextInt();
if (grade >= 90) {
numAs++;
} else if (grade >= 80 && grade <= 89) {
numBs++;
} else if (grade >= 70 && grade <= 79) {
numCs++;
} else if (grade <= 69) {
numFs++;
} else if (grade < 0 || grade > 100) {
System.out.println("Error invalid grade entered.");
}
And even if You add the missing {
, it doesn't solve multiple issues - what happens on an incorrect input; and how do you stop the loop correctly. So all things considered, this might be a bit better:
Scanner input = new Scanner(System.in);
int numAs = 0, numBs = 0, numCs = 0, numFs = 0;
int grade = 0;
String in = "";
while (true) {
System.out.println("Enter an exam grade or Q to exit");
//WHILE ENDS HERE
in = input.nextLine();
if ("Q".equals(in)) {
System.out.println("That's it!");
break;
}
try {
grade = Integer.parseInt(in); //get caught if not a num
if (grade < 0 || grade > 100) { //gets caught if bad num
throw new Exception();
}
} catch (Exception e) {
System.out.println("Error invalid grade entered.");
}
if (grade >= 90)
numAs++;
else if (grade >= 80 && grade <= 89)
numBs++;
else if (grade >= 70 && grade <= 79)
numCs++;
else if (grade <= 69)
numFs++;
System.out.println("Saved.");
}
//Continue after breaking the loop
Upvotes: 1
Reputation: 846
You are missing your curly braces after your while statement. If there are no curly braces specified, the while loop only includes the following statement, not the whole block.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int numAs = 0;
int numBs = 0;
int numCs = 0;
int numFs = 0;
int grade = 0;
while (grade >= 0 && grade <= 100){
System.out.println("Enter an exam grade");
grade = input.nextInt();
if (grade >= 90) {numAs++;
}
else if (grade >=80 && grade<= 89 ) {numBs++;
}
else if (grade >= 70 && grade <= 79) {numCs++;
}
else if (grade <= 69) {numFs++;
}
else if (grade < 0 || grade > 100) {System.out.println("Error invalid grade entered.");
}
}
}
Upvotes: 1