使用此代码(cpu 100%)后,我的Eclipse崩溃了,任何人都可以确认该代码是否有效。我不知道是否因为代码错误而崩溃
这是下面的代码
import java.util.Scanner;
public class ClassStudent {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int A=0;
int F=0;
int total=0;
int counter=0;
double Av12;
int pass=0;
int fail=0;
int grade;
System.out.println("enter grades or enter -1 to exit : ");
grade=input.nextInt();
while(grade>=0) {
if (grade>90 && grade<=100) {
A++;
pass++;
}
if (grade>0 && grade<=59) {
F++;
fail++;
}
total=total+grade;
counter++;
}
System.out.println("the total is "+total);
Av12=(double)total/counter;
System.out.printf("the averge is %.2f %n ",Av12);
System.out.printf("num of A is %d %n and F is %d%n and fail is %d%n and pass is %d%n ",A,F,fail,pass);
}
}
答案 0 :(得分:0)
我不是Java人士,但这应该可以。我可以提供这样的解决方案。
import java.util.Scanner;
public class ClassStudent {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int A=0;
int F=0;
int total=0;
int counter=0;
double Av12;
int pass=0;
int fail=0;
int grade;
do {
System.out.println("enter grades or enter -1 to exit : ");
grade=input.nextInt();
if (grade>90 && grade<=100) {
A++;
pass++;
}
if (grade>0 && grade<=59) {
F++;
fail++;
}
total=total+grade;
counter++;
} while(grade>=0);
System.out.println("the total is "+total);
Av12=(double)total/counter;
System.out.printf("the averge is %.2f %n ",Av12);
System.out.printf("num of A is %d %n and F is %d%n and fail is %d%n and pass is %d%n ",A,F,fail,pass);
}
}
答案 1 :(得分:0)
它是由于无限循环蚀显示了该错误。
我假设您要检查输入等级是否为有效输入。
假设输入等级应在0到100之间,
将while ( grade >= 0 )
更改为if ( grade >= 0 && grade <= 100 )
。