java - How to output a variable in a finally block? -


i trying condense code as possible, , trying output variable "grade" in instead of each case statement isn't recognizing variable. feel solution way easy cant figure out life of me. thank you.

code:

public class grade {//1  public static void main(string[] args) {//2       consolereader console = new consolereader(system.in);      boolean done = false;      system.out.println("enter grade percentage:");      do{         try{             int percent = (int) console.readdouble();             math.round(percent);             percent = (int) percent / 10;             string grade ="input not valid";              if(percent <= 5){//3                 grade = "your grade f, work harder won't have retake!";                 system.out.println(grade);                 done = true;              }else{//3//4             switch (percent){//5              case 6:                 grade = "your grade d, work harder";                 system.out.println(grade);                 done = true;                  break;             case 7:                 grade = "your grade c, that's average better.";                 system.out.println(grade);                 done = true;                  break;             case 8:                 grade = "your grade b, pretty strive a";                 system.out.println(grade);                 done = true;                  break;             case 9:                 grade = "your grade a, work!!";                 system.out.println(grade);                 done = true;                  break;             case 10:                 grade = "your grade a, work!!";                 system.out.println(grade);                 done = true;                  break;             default:                 grade = "your input invalid, please enter grade percentage.";                 system.out.println(grade);                 done = false;                  break;             }//5             }//4          }catch(numberformatexception e){             system.out.println("please input numbers, try again:");         }finally{             if(done == true){         //error system.out.println(grade); //error             system.out.println("i hope you're happy grade!");             }else{              }         }     }while(done == false);        }//2 }//1 

you've defined grade inside try block, it's scope limited of block itself. it's not accessible outside try, e.g. in finally.

to make accessible in finally, declare grade before try block, scope entire method.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -