[英]Java Do-While Try-Catch exception handling loop error and symbol: variable error
固定(下面的新工作代码)。 我遇到了一个我无法弄清楚的小问题。 第一个 do-while 按预期工作,并将 go 通过,直到它获得有效输入。 但是,无论我输入有效还是无效的 #,第二个循环都会结束。 然后在第三个循环中,我得到2 个错误:
我不确定为什么在已经声明变量时会出现这些错误?
关于我正在尝试做的一点解释:
我应该得到 3 个输入:多年的经验 (yearsexp)、性能 (performance) 和在 1-10(level) 之间生成的随机 int。 该程序将询问用户他们的体验是否在 3-11 之间他们是合格的。 如果没有,它会告诉他们他们不合格并要求重新输入一个值。 性能也一样。 如果他们输入一个小于或等于 11 的数字,它将继续生成随机 int(级别),在该点级别将用于评估他们的奖金。
import java.util.Scanner;
import java.util.Random;
class assignment {
public static void main( String args[]){
//
int bonus;
int x;
//
Random rand = new Random();
//
Scanner input = new Scanner(System.in); // scanner for name
System.out.println("Enter your name: "); // ask for name
String name = input.nextLine(); // assign name to variable
System.out.println( name ); // output name
//
Scanner input2 = new Scanner(System.in); // scanner for yearsexp
System.out.println(name + ", Enter the years of your experience: "); // ask for yearsexp
int yearsexp = 0 ;
int performance = 0;
do
{
yearsexp = input2.nextInt(); // assign yearsexp to variable
System.out.println( yearsexp); // out yearsexp
try
{
if (yearsexp >= 3 && yearsexp <24)// acceptable critera
{
System.out.println("You are qualified");// output
x=2; // x = 2 to close do while
}
else
{
throw new Exception ();
}
}
catch (Exception e1)
{
System.out.println("You have entered an invalid number! Try again..."); //output is unacceptable
x=1;
}
}
while (x==1); // keep do loop going if exception e
//
Scanner input3 = new Scanner(System.in); // scanner for performance
System.out.println(name + ", Enter the performance: "); // ask for performance
//
do
{
performance = input3.nextInt(); // assign performance to variable
System.out.println( performance ) ; // output performance
try
{
if (performance <= 11) // acceptable criteria
{
System.out.println("You are qualified"); //output
x=2; // x = 2 to close do while
}
else
{
throw new Exception ();
}
}
catch (Exception e2)
{
System.out.println("You have entered an invalid number! Try again..."); //output if criteria is unacceptable
x=1;
}
}
while (x==1); // keep doing while
//
int level = rand.nextInt(11); // fix to be between 1-10 to not include 0
System.out.println("Random Level: " + level); //output level
//
do
{
try
{
if (level >=5 && level <=8)//acceptable criteria
{
System.out.println("Expected Bonus: $5000.00"); //output
x = 2; // x = 2 to close while
}
else if (level <= 4) // else if criteria
{
bonus = 5000 * yearsexp * performance * level; // calculate bonus if level <=4
System.out.println(bonus); // output bonus
if (bonus < 15000)
{
System.out.println(bonus);
}
else if (bonus > 15000)
{
System.out.println("Your bonus is TBL");
{
x = 2; // x = 2 to close while
}
}
}
}
catch (Exception e3)
{
System.out.println("You do not get a bonus"); //output if criteria is unacceptable
x=1;
}
}
while (x==1);
}
}
您收到这些编译错误的原因是因为performance
仅适用于您的第二个 do/while 循环的try
语句。
如果您需要performance
和yearsexp
值的“知识”从第二个 do/while 循环传播到第三个,您需要在两个循环之前声明变量,以便它们的范围对两者都可见。
go 更详细一点,
if (foo()) {
int myValue = 1;
}
System.out.println(myValue);
不起作用,因为myValue
变量的作用域是 if 语句,这意味着就 println 语句而言,它不存在。 对于for
、 while
、 do/while
和try/catch
语句也是如此。
try {
String a = "hello";
} catch (Exception e) {
}
System.out.println(a);
也不行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.