已修复(下面有新的工作代码)。我遇到了一个无法解决的小问题。第一个do-while会按预期工作,并将一直进行到获得有效输入为止。但是,无论我输入的是有效还是无效的#,第二个循环都将结束。然后在第3个循环中,出现 2个错误:
我不确定在声明变量后为什么会出现这些错误?
关于我要做什么的一些解释:
我应该得到3个输入:多年的经验(yearsexp),性能(性能)和在1-10(级别)之间生成的随机整数。该程序将询问用户他们的经验,如果介于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);
}
}
答案 0 :(得分:1)
出现这些编译错误的原因是,performance
仅限于第二个do / while循环的try
语句。
如果您需要'{'1}}和performance
的值的知识来从第二个do / while循环传播到第三个do / while循环,则需要在两个循环之前声明变量,以便它们被限定为对双方都可见。
要详细一点,
yearsexp
不起作用,因为if (foo()) {
int myValue = 1;
}
System.out.println(myValue);
变量的作用域为if语句,这意味着就println语句而言,它不存在。 myValue
,for
,while
和do/while
语句也是如此。
try/catch
也不起作用。