使用读取的switch语句名称GradeReport编写程序 来自用户的成绩并相应地打印评论。
说明:提示用户输入他们的成绩用户必须提示 如果输入不是整数,则输入数字等级(0到100) 在0到100之间,提示用户重新输入
如果不满足条件,请使用do循环重新输入等级 或等级不在0到100之间,然后在屏幕上提示用户 如下(“错误:您必须输入0到100之间的整数”);“
还包含一个while循环来跟踪尝试。创建一个 变量名称“尝试”将跟踪尝试不超过3 因此,如果输入不是0到100之间的整数,则提示输入 用户重新输入
我有一个问题是找出while循环以跟踪尝试。
这就是我所拥有的:
import java.util.Scanner;
public class GradeReport {
public static void main(String[] args) {
int grade, category;
Scanner scan = new Scanner(System.in);
do {
System.out.println("Enter a numeric grade (0 to 100): ");
grade = scan.nextInt();
if (grade >= 0 && grade <= 100)
break;
System.out.println("Error: You must re-enter an integer between 0 and 100");
} while (true);
int tries = 1;
while (tries <= 3) {
System.out.println("Tries: " + tries);
tries++;
category = grade / 10;
System.out.print("That grade is ");
switch (category) {
case 10:
System.out.println("a perfect score. Well done.");
break;
case 9:
System.out.println("well above average. Excellent.");
break;
case 8:
System.out.println("above average. Nice job.");
break;
case 7:
System.out.println("average.");
break;
case 6:
System.out.println("below average. You should see the");
System.out.println("instructor to clarify the material " + "presented in class.");
break;
default:
System.out.println("not passing.");
}
}
}
}
答案 0 :(得分:0)
这是您尝试的部分:
do {
System.out.println("Enter a numeric grade (0 to 100): ");
grade=scan.nextInt();
if(grade>=0 && grade<=100)
break;
System.out.println ("Error: You must re-enter an integer between 0 and 100");
}while(true);
这是您认为计算尝试次数的部分:
int tries = 1;
while (tries <= 3) {
System.out.println("Tries: " + tries);
tries++;
...
You need to define `int tries = 0;` before trying, and integrate the incrementation inside the loop where you actually try. I'm sure you can figure out the rest yourself. You seem to be pretty close :)
编辑:由于其他人试图给你完整答案(而且不太好),我会这样做: import java.util.Scanner;
public class GradeReport {
public static void main(String[] args) {
int grade, category;
int tries = 0;
Scanner scan = new Scanner(System.in);
do {
tries++;
System.out.println("Enter a numeric grade (0 to 100): ");
grade = scan.nextInt();
if(grade >= 0 && grade <= 100){
break;
}
System.out.println("attempt " + tries + " unsuccessful");
if(tries >= 3){
System.out.println("No more attempts.");
break;
}
System.out.println("Error: You must re-enter an integer between 0 and 100");
} while (true);
if(grade >= 0 && grade <= 100){
category = grade / 10;
System.out.print("That grade is ");
switch (category) {
case 10:
System.out.println("a perfect score. Well done.");
break;
case 9:
System.out.println("well above average. Excellent.");
break;
case 8:
System.out.println("above average. Nice job.");
break;
case 7:
System.out.println("average.");
break;
case 6:
System.out.println("below average. You should see the");
System.out.println("instructor to clarify the material " + "presented in class.");
break;
default:
System.out.println("not passing.");
}
}
}
}
答案 1 :(得分:0)
您只需在循环输入之前设置tries
变量:
Scanner scan = new Scanner (System.in);
int tries = 0;
do
{
System.out.println("Enter a numeric grade (0 to 100): ");
grade=scan.nextInt();
if(grade>=0 && grade<=100)
break;
tries += 1;
System.out.println ("Error: You must re-enter an integer between 0 and 100");
}while(tries < 3);
System.out.println("Tries: " + (tries + 1));
答案 2 :(得分:0)
试试此代码
int grade, category;
Scanner scanner = new Scanner(System.in);
int tries = 0;
do{
System.out.print("Enter a numeric grade (0 to 100): ");
grade = scanner.nextInt();
if(grade<0 || grade>100){
System.out.println ("Error: You must re-enter an integer between 0 and 100");
} else {
break;
}
tries++;
} while (tries < 3);
System.out.print(grade);
在您拥有以下代码的地方
int grade, category;
Scanner scan = new Scanner (System.in);
do
{
System.out.println("Enter a numeric grade (0 to 100): ");
grade=scan.nextInt();
if(grade>=0 && grade<=100)
break;
System.out.println ("Error: You must re-enter an integer between 0 and 100");
}while(true);
int tries = 1;
while (tries <= 3) {
System.out.println("Tries: " + tries);
tries++;