使用此代码(cpu 100%)后,我的 Eclipse 崩溃,任何人都可以确认该代码是否有效

我的 Eclipse 在使用此代码 (cpu 100% ) 后崩溃,任何人都可以确认代码是否有效。我不知道是否因为代码错误而崩溃


这是下面的代码


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);


    }


}


子衿沉夜
浏览 162回答 2
2回答

qq_花开花谢_0

其因为无限循环 eclipse 显示该错误。我假设您想检查输入等级是否为有效条目。假设输入等级应该在 0 到 100 之间,更改while ( grade >= 0 )为if ( grade >= 0 && grade <= 100 ).

qq_笑_17

我不是 Java 人,但这应该可行。我可以提供这样的解决方案。import java.util.Scanner;public class ClassStudent {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; Scanner input = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; int A=0;&nbsp; &nbsp; &nbsp; &nbsp; int F=0;&nbsp; &nbsp; &nbsp; &nbsp; int total=0;&nbsp; &nbsp; &nbsp; &nbsp; int counter=0;&nbsp; &nbsp; &nbsp; &nbsp; double Av12;&nbsp; &nbsp; &nbsp; &nbsp; int pass=0;&nbsp; &nbsp; &nbsp; &nbsp; int fail=0;&nbsp; &nbsp; &nbsp; &nbsp; int grade;&nbsp; &nbsp; &nbsp; &nbsp; do {&nbsp; &nbsp; System.out.println("enter grades or enter -1 to exit&nbsp; :&nbsp; ");&nbsp; &nbsp; &nbsp; &nbsp;grade=input.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (grade>90 && grade<=100) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; A++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pass++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (grade>0 && grade<=59) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; F++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fail++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; total=total+grade;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter++;&nbsp; &nbsp; &nbsp; &nbsp; } while(grade>=0);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("the total is "+total);&nbsp; &nbsp; &nbsp; &nbsp; Av12=(double)total/counter;&nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("the averge is %.2f %n ",Av12);&nbsp; &nbsp; &nbsp; &nbsp; 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);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java