Java 如何在finally块中输出变量?
我试图尽可能地压缩代码,并试图在finally而不是每个case语句中输出变量“Grade”,但它无法识别变量。 我觉得解决这个问题太简单了,但我一辈子都想不出来。 多谢各位 代码: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;
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
,对吗?