提问人: 提问时间:10/14/2023 最后编辑:Iłya Bursov 更新时间:10/15/2023 访问量:67
为什么我无法获得分类的输出?
why i cant get the output for categorize?
问:
我必须制作一个 Java 程序来接收学生数据并停在 -1,然后显示成绩数及其平均值,最后,它将显示类别 A、B、C、D 的成绩。此代码仅在显示类别时出现错误。我需要帮助。在其中,我将输入所需的输出
import java.util.Scanner;
public class Grades {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("enter 5 grades and then -1 to quit!");
System.out.print("enter grade:");
double grade = input.nextDouble();
double total = 0;
int count = 0;
int A = 0;
int B = 0;
int C = 0;
int D = 0;
int F = 0;
while (grade != -1) {
total = total + grade;
System.out.print("enter grade:");
grade = input.nextDouble();
count++;
}
System.out.println("the number of grades= " + count);
System.out.println("Average: " + total / count);
if (grade >= 90 && grade <= 100) {
A++;
} else if (grade >= 80 && grade <= 89) {
B++;
} else if (grade >= 70 && grade <= 79) {
C++;
} else if (grade >= 60 && grade <= 69) {
D++;
} else if (grade >= 0 && grade <= 59) {
F++;
}
System.out.println("number of A grades :" + A);
System.out.println("number of B grades :" + B);
System.out.println("number of C grades :" + C);
System.out.println("number of D grades :" + D);
System.out.println("number of F grades :" + F);
}
}
输出应为:
输入等级:90 输入等级:99 输入等级:97 输入等级:98 输入等级:87 输入等级:-1 等级数= 5 平均:94.2
A级总数:4 B级总数:1 C级总数:0 D级总数:0 F级总数:0
但相反,我得到了这个:
输入等级:90 输入等级:99 输入等级:97 输入等级:98 输入等级:87 输入等级:-1 等级数= 5 平均:94.2
A级总数:0 B级总数:0 C级总数:0 D级总数:0 F级总数:0
有人可以告诉我为什么它不起作用吗?
答:
2赞
Jeet Kumar
10/14/2023
#1
代码的问题在于,您在 while 循环完成后检查成绩,这意味着该变量在该点包含 -1 的值,并且您只计算成绩类别的一个值。在阅读成绩时,您应该检查循环中的类别。以下是更正后的代码:grade
import java.util.Scanner;
public class Grades {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter 5 grades and then -1 to quit!");
double total = 0;
int count = 0;
int A = 0;
int B = 0;
int C = 0;
int D = 0;
int F = 0;
while (true) {
System.out.print("Enter grade (or -1 to quit): ");
double grade = input.nextDouble();
if (grade == -1) {
break; // Exit the loop when -1 is entered
}
total += grade;
count++;
if (grade >= 90 && grade <= 100) {
A++;
} else if (grade >= 80 && grade <= 89) {
B++;
} else if (grade >= 70 && grade <= 79) {
C++;
} else if (grade >= 60 && grade <= 69) {
D++;
} else if (grade >= 0 && grade <= 59) {
F++;
}
}
System.out.println("The number of grades = " + count);
System.out.println("Average: " + total / count);
System.out.println("Number of A grades: " + A);
System.out.println("Number of B grades: " + B);
System.out.println("Number of C grades: " + C);
System.out.println("Number of D grades: " + D);
System.out.println("Number of F grades: " + F);
}
}
使用此代码,您可以在循环中收到每个成绩时检查成绩类别,它将产生所需的输出。
0赞
nokla
10/14/2023
#2
当您退出循环并到达零件时,值为 因此,任何条件都不会为真。if-else
grade
-1
您需要将零件移动到循环中:if-else
while
while (grade!=-1){
total += grade;
if(grade>=90 && grade<=100){
A++;}
else if(grade>=80 && grade<=89){
B++;}
else if(grade>=70 && grade<=79){
C++;}
else if(grade>=60 && grade<=69){
D++;}
else if(grade>=0 && grade<=59){
F++;}
count++;
System.out.print("enter grade:");
grade=input.nextDouble();
}
输出:
enter 5 grades and then -1 to quit!
enter grade:90
enter grade:99
enter grade:97
enter grade:98
enter grade:87
enter grade:-1
the number of grades= 5
Average: 94.2
number of A grades :4
number of B grades :1
number of C grades :0
number of D grades :0
number of F grades :0
编辑:
import java.util.Scanner;
public class Grades {
public static void main(String []args){
Scanner input = new Scanner(System.in);
System.out.println("enter 5 grades and then -1 to quit!");
System.out.print("enter grade:");
double grade=input.nextDouble();
double total=0;
int count=0;
int A= 0;
int B= 0;
int C = 0;
int D= 0;
int F = 0;
while (grade!=-1){
total += grade;
if(grade>=90 && grade<=100){
A++;}
else if(grade>=80 && grade<=89){
B++;}
else if(grade>=70 && grade<=79){
C++;}
else if(grade>=60 && grade<=69){
D++;}
else if(grade>=0 && grade<=59){
F++;}
count++;
System.out.print("enter grade:");
grade=input.nextDouble();
}
System.out.println("the number of grades= "+ count);
System.out.println("Average: " + total/count);
System.out.println("number of A grades :"+ A);
System.out.println("number of B grades :"+ B);
System.out.println("number of C grades :"+ C);
System.out.println("number of D grades :"+ D);
System.out.println("number of F grades :"+ F);
}
}
评论
0赞
Iłya Bursov
10/14/2023
如果部分应该在之前,否则将跳过第一个值读取(在 main 的最开头读取)nextDouble
0赞
nokla
10/14/2023
@I łyaBursov 是的,我只是忘记保存编辑:)
0赞
10/14/2023
感谢您的回答,我真的很高兴您帮助了我!我做了你告诉我的和它的作品!但是有一个错误,它给了我 6 个等级,而我以 -1 输入了 5 个等级,那么我该如何解决这个问题?
0赞
nokla
10/14/2023
@sera 嗨,我附上了输出..
0赞
10/14/2023
很抱歉打扰了,但它仍然给了我 6 而不是 5 <\\3
1赞
Reilas
10/14/2023
#3
"...此代码仅在显示类别时出现错误。...
。有人可以告诉我为什么它不起作用吗?
发生这种情况的原因是您正在检查 grade 的值,在循环之后,该值为 −1。
此外,您还应该统一收集输入值。
并且,重构等级 if 语句。
Scanner input = new Scanner(System.in);
System.out.println("enter 5 grades and then -1 to quit!");
double grade;
double total=0;
int count=0;
int A= 0;
int B= 0;
int C = 0;
int D= 0;
int F = 0;
boolean exit = false;
do {
System.out.print("enter grade:");
grade=input.nextDouble();
if (grade == -1) exit = true;
else {
total = total + grade;
count++;
if(grade>=90) A++;
else if(grade>=80) B++;
else if(grade>=70) C++;
else if(grade>=60) D++;
else F++;
}
} while (!exit);
System.out.println("the number of grades= "+ count);
System.out.println("Average: " + total/count);
System.out.println("number of A grades :"+ A);
System.out.println("number of B grades :"+ B);
System.out.println("number of C grades :"+ C);
System.out.println("number of D grades :"+ D);
System.out.println("number of F grades :"+ F);
输出
enter 5 grades and then -1 to quit!
enter grade:90
enter grade:80
enter grade:70
enter grade:-1
the number of grades= 3
Average: 80.0
number of A grades :1
number of B grades :1
number of C grades :1
number of D grades :0
number of F grades :0
评论
0赞
10/14/2023
谢谢kkkkk yoooouuu,我哭了,它!!
上一个:不产生任何输出?
评论
grade
while (grade!=-1)