对于我的程序,我想要一个询问学生人数,询问分数的东西,然后显示其中有多少分数是A,B,C等的输出。因此,如果我输入5个学生,我应该能够输入这5个分数,然后它将显示相应的成绩。取而代之的是,它在输入一个分数之后显示相应的分数,然后要求另一个分数。
//Declarations
int A=0,B=0,C=0,D=0,F=0, score,I, students;
I=1;
System.out.println("How many students are in your class: ");
students = input.nextInt();
while (I<=students) {
System.out.println("enter a score:");
score=input.nextInt();
if(score >= 90 && score <= 100)
A++;
else if(score>=80 && score<=89)
B++;
else if(score>=70 && score<=79)
C++;
else if(score>=60 && score<=69)
D++;
else if(score>=0 && score<=59)
F++;
System.out.println("Total number of A's:"+ A);
System.out.println("Total number of B's:"+ B);
System.out.println("Total number of C's:"+ C);
System.out.println("Total number of D's:"+ D);
System.out.println("Total number of F's:"+ F);
}
}}
答案 0 :(得分:0)
简而言之-while循环的范围需要更小:
详细信息:对于每个学生,您要处理他们的考试成绩, 增加几个变量之一。完全完成操作后(例如退出循环!)-您想要总结结果。
//Declarations
int A=0,B=0,C=0,D=0,F=0, score,I, students;
I=1;
System.out.println("How many students are in your class: ");
students = input.nextInt();
while (I<=students) { // <-- separate unrelated problem where you aren't iterating properly
System.out.println("enter a score:");
score=input.nextInt();
if(score >= 90 && score <= 100)
A++;
else if(score>=80 && score<=89)
B++;
else if(score>=70 && score<=79)
C++;
else if(score>=60 && score<=69)
D++;
else if(score>=0 && score<=59)
F++;
}
I++;
} // <- the while loop should end here
System.out.println("Total number of A's:"+ A);
System.out.println("Total number of B's:"+ B);
System.out.println("Total number of C's:"+ C);
System.out.println("Total number of D's:"+ D);
System.out.println("Total number of F's:"+ F);
}
答案 1 :(得分:0)
您的代码中有两个问题:
System.out.println
内有while
这是完成的方式
int A = 0, B = 0, C = 0, D = 0, F = 0, score, I, students;
I = 1;
Scanner input = new Scanner(System.in);
System.out.println("How many students are in your class: ");
students = input.nextInt();
while (I <= students) {
System.out.println("enter a score:");
score = input.nextInt();
if (score >= 90 && score <= 100)
A++;
else if (score >= 80 && score <= 89)
B++;
else if (score >= 70 && score <= 79)
C++;
else if (score >= 60 && score <= 69)
D++;
else if (score >= 0 && score <= 59)
F++;
students--;
}
System.out.println("Total number of A's:" + A);
System.out.println("Total number of B's:" + B);
System.out.println("Total number of C's:" + C);
System.out.println("Total number of D's:" + D);
System.out.println("Total number of F's:" + F);
我添加了students--
,因为它会减少您得分的学生人数,因此当学生用完时,它将退出while循环
答案 2 :(得分:0)
感觉在循环括号中放错了位置。 将括号(while循环的右括号)放在f ++之后。 希望这能解决您的问题。 还有学生-应该在那里。
我建议您使用for循环,我发现它对于初学者来说要简单得多。
for( int i=0; i < students; i++) {
//statements here
}
答案 3 :(得分:0)
您正在每个循环中打印所需的输出,因此请将打印语句放置在循环外部。另外,当前循环是无限的,因此在每个循环中增加 I 的值或减小学生的值。您的代码应类似于:
private slots:
void zoomBarAct(int zoom);
答案 4 :(得分:0)
尝试一下:
{
int A=0,B=0,C=0,D=0,F=0, score;
Scanner s = new Scanner(System.in);
System.out.println("How many students are in your class: ");
int numberOfStudents = s.nextInt();
for (int i = 0 ; i < numberOfStudents ; i++) {
System.out.println("enter score from 0 to 100 for student number " + (i + 1) + " :");
score =s.nextInt();
if(score >= 90 && score <= 100)
A++;
else if(score>=80 && score<=89)
B++;
else if(score>=70 && score<=79)
C++;
else if(score>=60 && score<=69)
D++;
else if(score>=0 && score<=59)
F++;
}
System.out.println("Total number of A's:"+ A);
System.out.println("Total number of B's:"+ B);
System.out.println("Total number of C's:"+ C);
System.out.println("Total number of D's:"+ D);
System.out.println("Total number of F's:"+ F);
}