Java 如何在finally块中输出变量?

Java 如何在finally块中输出变量?,java,exception-handling,try-catch,Java,Exception Handling,Try Catch,我试图尽可能地压缩代码,并试图在finally而不是每个case语句中输出变量“Grade”,但它无法识别变量。 我觉得解决这个问题太简单了,但我一辈子都想不出来。 多谢各位 代码: public class Grade {//1 public static void main(String[] args) {//2 ConsoleReader console = new ConsoleReader(System.in); boolean done = false;

我试图尽可能地压缩代码,并试图在finally而不是每个case语句中输出变量“Grade”,但它无法识别变量。 我觉得解决这个问题太简单了,但我一辈子都想不出来。 多谢各位

代码:

public class Grade {//1

public static void main(String[] args) {//2


    ConsoleReader console = new ConsoleReader(System.in);

    boolean done = false;

    System.out.println("Enter your grade percentage:");

    do{
        try{
            int percent = (int) console.readDouble();
            Math.round(percent);
            percent = (int) percent / 10;
            String grade ="Input was not valid";

            if(percent <= 5){//3
                grade = "Your grade is an F, Work Harder so you won't have to retake!";
                System.out.println(grade);
                done = true;

            }else{//3//4
            switch (percent){//5

            case 6:
                grade = "Your grade is a D, work harder";
                System.out.println(grade);
                done = true;

                break;
            case 7:
                grade = "Your grade is a C, That's average but you could do better.";
                System.out.println(grade);
                done = true;

                break;
            case 8:
                grade = "Your grade is a B, That is pretty good but strive for that A";
                System.out.println(grade);
                done = true;

                break;
            case 9:
                grade = "Your grade is a A, Good Work!!";
                System.out.println(grade);
                done = true;

                break;
            case 10:
                grade = "Your grade is a A, Good Work!!";
                System.out.println(grade);
                done = true;

                break;
            default:
                grade = "Your input was invalid, Please enter your grade percentage.";
                System.out.println(grade);
                done = false;

                break;
            }//5
            }//4

        }catch(NumberFormatException e){
            System.out.println("Please input only numbers, try again:");
        }finally{
            if(done == true){
        //ERROR System.out.println(grade); //ERROR
            System.out.println("I hope you're happy with your grade!");
            }else{

            }
        }
    }while(done == false);



    }//2
}//1
公共课等级{//1
公共静态void main(字符串[]参数){//2
ConsoleReader控制台=新的ConsoleReader(System.in);
布尔完成=假;
System.out.println(“输入你的分数百分比:”);
做{
试一试{
int percent=(int)console.readDouble();
数学四舍五入(百分比);
百分比=(整数)百分比/10;
String grade=“输入无效”;

如果(百分比您在
try
块中定义了
等级
,则其范围仅限于块本身的范围。在
try
之外,例如在
finally
中,无法访问该范围


要在
try
块中访问它,请在
try
块之前声明
grade
,这样它的范围就是整个方法。

您已经在
try
块中定义了
grade
,所以它的范围仅限于块本身的范围。在
try
块之外是不可访问的,例如在
try块中e> 


要使其在
finally
中可访问,请在
try
块之前声明
grade
,因此其范围是整个方法。

在try块之前声明变量grade

 String grade ="Input was not valid";

在try块之前声明变量grade

 String grade ="Input was not valid";

您必须在try-catch块之外声明等级字符串,然后才能在finally语句中使用它

试着这样做:

String grade ="Input was not valid";
try{
        int percent = (int) console.readDouble();
        Math.round(percent);
        percent = (int) percent / 10;


        if(percent <= 5){//3
            grade = "Your grade is an F, Work Harder so you won't have to retake!";
            System.out.println(grade);
            done = true;

        }else{//3//4
        switch (percent){//5

        case 6:
            grade = "Your grade is a D, work harder";
            System.out.println(grade);
            done = true;

            break;
        case 7:
            grade = "Your grade is a C, That's average but you could do better.";
            System.out.println(grade);
            done = true;

            break;
        case 8:
            grade = "Your grade is a B, That is pretty good but strive for that A";
            System.out.println(grade);
            done = true;

            break;
        case 9:
            grade = "Your grade is a A, Good Work!!";
            System.out.println(grade);
            done = true;

            break;
        case 10:
            grade = "Your grade is a A, Good Work!!";
            System.out.println(grade);
            done = true;

            break;
        default:
            grade = "Your input was invalid, Please enter your grade percentage.";
            System.out.println(grade);
            done = false;

            break;
        }//5
        }//4

    }catch(NumberFormatException e){
        System.out.println("Please input only numbers, try again:");
    }finally{
        if(done == true){
    //ERROR System.out.println(grade); //ERROR
        System.out.println("I hope you're happy with your grade!");
        }else{

        }
    }
String grade=“输入无效”;
试一试{
int percent=(int)console.readDouble();
数学四舍五入(百分比);
百分比=(整数)百分比/10;

如果(百分比您必须在try-catch块之外声明等级字符串,那么您可以在finally语句中使用它

试着这样做:

String grade ="Input was not valid";
try{
        int percent = (int) console.readDouble();
        Math.round(percent);
        percent = (int) percent / 10;


        if(percent <= 5){//3
            grade = "Your grade is an F, Work Harder so you won't have to retake!";
            System.out.println(grade);
            done = true;

        }else{//3//4
        switch (percent){//5

        case 6:
            grade = "Your grade is a D, work harder";
            System.out.println(grade);
            done = true;

            break;
        case 7:
            grade = "Your grade is a C, That's average but you could do better.";
            System.out.println(grade);
            done = true;

            break;
        case 8:
            grade = "Your grade is a B, That is pretty good but strive for that A";
            System.out.println(grade);
            done = true;

            break;
        case 9:
            grade = "Your grade is a A, Good Work!!";
            System.out.println(grade);
            done = true;

            break;
        case 10:
            grade = "Your grade is a A, Good Work!!";
            System.out.println(grade);
            done = true;

            break;
        default:
            grade = "Your input was invalid, Please enter your grade percentage.";
            System.out.println(grade);
            done = false;

            break;
        }//5
        }//4

    }catch(NumberFormatException e){
        System.out.println("Please input only numbers, try again:");
    }finally{
        if(done == true){
    //ERROR System.out.println(grade); //ERROR
        System.out.println("I hope you're happy with your grade!");
        }else{

        }
    }
String grade=“输入无效”;
试一试{
int percent=(int)console.readDouble();
数学四舍五入(百分比);
百分比=(整数)百分比/10;

如果(百分比)您的意思是,在try-catch块之外声明
grade
,对吗?您的意思是,在try-catch块之外声明
grade
,对吗?