如何在finally块中输出变量?

时间:2014-03-10 18:28:39

标签: java exception-handling try-catch

我试图尽可能地压缩我的代码,并且我试图在finally中输出我的变量“Grade”而不是每个case语句但是它没有识别变量。 我觉得解决这个问题的方法太简单了,但我无法想象我的生活。 谢谢。

代码:

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

3 个答案:

答案 0 :(得分:5)

您已在grade块中定义了try,因此其范围仅限于块本身的范围。它不能在try之外访问,例如在finally

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

答案 1 :(得分:0)

在try块之前声明变量 grade

 String grade ="Input was not valid";

答案 2 :(得分:0)

您必须在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{

        }
    }