【问题标题】:Java Do-While Try-Catch exception handling loop error and symbol: variable errorJava Do-While Try-Catch 异常处理循环错误和符号:变量错误
【发布时间】:2019-10-08 14:31:00
【问题描述】:

已修复(以下是新的工作代码)。我遇到了一个我无法弄清楚的小问题。第一个 do-while 会按预期工作,并且会一直执行,直到获得有效输入。但是,无论我输入有效还是无效的#,第二个循环都会结束。然后在第三个循环中,我得到 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); 

       }
  }

【问题讨论】:

  • 您不需要声明使用System.inScanner 的多个实例。您可以继续使用同一个。
  • 您在 try 块中声明 yearsexp 和“性能”。这会导致 try 块成为该变量的 scope,并且变量不存在于该范围之外。您需要在 try 块之外声明变量,以便它们在所有 try 块中可见。

标签: java exception try-catch do-while


【解决方案1】:

您收到这些编译错误的原因是因为 performance 仅适用于您的第二个 do/while 循环的 try 语句。

如果您需要“了解”performanceyearsexp 的值以从第二个 do/while 循环传播到第三个循环,则需要在两个循环之前声明变量,以便它们的范围为对双方都可见。

要详细一点,

if (foo()) {
  int myValue = 1;
}
System.out.println(myValue);

不起作用,因为 myValue 变量的作用域是 if 语句,这意味着就 println 语句而言,它不存在。 forwhiledo/whiletry/catch 语句也是如此。

try {
  String a = "hello";
} catch (Exception e) {
}

System.out.println(a);

也不行。

【讨论】:

  • 另外,他的yearsexp 范围也不正确。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-06
  • 1970-01-01
  • 2019-06-29
相关资源
最近更新 更多