【问题标题】:switch with loop用循环切换
【发布时间】:2013-01-19 18:52:55
【问题描述】:

我正在尝试使用 switch 语句创建一个循环。如果用户未在 1 和 4 之间输入,则会再次出现“您输入的选项不正确”的消息。我正在制作 Lynda 视频。我不确定在哪里放置循环。我目前找不到让它循环的方法。是在 switch 中还是在 getInput 方法中。是否有可能做到这一点?如果有人知道,请提前感谢。我正在使用 Eclipse,Java 7。

  public static void main(String[] args) {
    String s1 = getInput("Enter a numeric value: ");
    String s2 = getInput("Enter a numeric value: ");



  double result = 0;
   do  { 
           String op = getInput("Enter 1=Add, 2=Subtract, 3=Multiply, 4=Divide");
           int opInt = Integer.parseInt(op);
           switch (opInt) 
           {
             case 1:
             result = addValues(s1, s2);
             break;
           }
        } while(opInt<1 || opInt >4);

编辑 错误信息...

此行有多个标记 - opInt 无法解析为 变量

  • opInt 无法解析为 变量

    //在其他数学运算符中,我有一个名为 addValues 的方法

    private static double addValues(String s1, String s2)
    throws NumberFormatException {
    double d1 = Double.parseDouble(s1);
    double d2 = Double.parseDouble(s2);
    double result = d1 + d2;
    return result;
    }
    
    private static String getInput(String prompt) {
    BufferedReader stdin = new BufferedReader(
            new InputStreamReader(System.in));
    
    System.out.print(prompt);
    System.out.flush();
    
    try {
        return stdin.readLine();
    } catch (Exception e) {
        return "Error: " + e.getMessage();
    }
    }
    

    编辑 我对 Tal 和 Quoi 给我的解决方案有疑问。使用 Quoi 我收到 opInt 的错误无法解析为变量,因为使用 Tal 没有任何反应。

所以我做了以下...

String s1 = getInput("Enter a numeric value: ");
    String s2 = getInput("Enter a numeric value: ");
    String op = getInput("Enter 1=Add, 2=Subtract, 3=Multiply, 4=Divide");

//convert opInt into integer
    int opInt = Integer.parseInt(op);

    if (opInt <1 || opInt >4) // used a if statement
     {
      getInput("Enter 1=Add, 2=Subtract, 3=Multiply, 4=Divide");
     }

    double result = 0;
    {
         switch (opInt)
         {
           ...........
           ...........
         }
    }

使用 if 语句,该方法会回调 switch 语句,但在选择正确的选项后,会显示“您输入的选项不正确”和“答案是 0.0”的打印行,因此不进行计算。我不确定这是一个简单的解决方案还是什么?

编辑 我现在尝试在开关外做一个 while 循环。发生的事情是,当我在 1 或 4 之外选择时,无论我是否输入有效选项,我都会收到“输入 1=加,2=减,3=乘,4=除”的消息。

do 
{
  getInput("Enter 1=Add, 2=Subtract, 3=Multiply, 4=Divide");
 } while (opInt <1 || opInt >4);

    double result = 0;
    {
       switch (opInt)
       {
           case 1:
            result = addValues(s1, s2);
            break;
             ...........
             ...........
         }
    }

【问题讨论】:

  • 为什么不把switch放在一个循环里面?
  • @MrSmith42 - 我以为我做到了。我已经有一段时间没来了,所以我可能错过了,或者这里发生了一些变化。

标签: java eclipse switch-statement


【解决方案1】:

switch-case 放入循环中。

do{
   String op = getInput("Enter 1=Add, 2=Subtract, 3=Multiply,  
       4=Divide");
   int opInt = Integer.parseInt(op);

   switch(opInt){
      case 1: ... break;
      case 2: ... break;
      case 3: ... break;
      case 4: ... break;
   }
}while(opInt<1 || opInt >4);

【讨论】:

  • 然后在每种情况下设置一个布尔变量,以便当一个情况执行完毕时,while循环知道结束。
  • @Chiel92 你的意思是布尔真/假?如果这就是你的意思,我也需要对此进行研究。
  • @DiscoDude 他正在尝试为具有额外布尔值的循环创建条件。但是可以在没有任何额外变量的情况下创建条件。
  • @Quoi - 我在这一行看到多个标记的错误 - opInt 无法解析为变量。这是为什么呢?
【解决方案2】:

将它放在最后一个 getInput 调用周围,即读取用户选项的调用。

乐:

这就是我最初的意思。

 String s1 = getInput("Enter a numeric value: ");
 String s2 = getInput("Enter a numeric value: ");

 int opInt = 0;
 do {
   String op = getInput("Enter 1=Add, 2=Subtract, 3=Multiply, 4=Divide");
   try {
     opInt = Integer.parseInt(op); 
   }catch(Exception e) {
     //whatever you need to do
   }
 } while (opInt < 1 || opInt > 4);

 switch(opInt) [...]

这里发生了什么:用户输入 2 个数字,然后系统会要求输入他的选项,直到它介于 1 和 4 之间。

【讨论】:

  • 好吧,我现在对你想要达到的目标有点困惑。您希望用户能够连续添加/减去等,还是希望用户选择一次操作,进行演算,显示结果并退出?
  • 用户所做的只是输入两次值,然后选择所有工作的简单计算。用户有 4 个选项,即 1-加法、2-减法、3-乘法和 4-除法。这也很好。现在,如果用户输入的选项小于 1 或更多 4,那么我正在努力解决的 getInput 方法(“Enter 1=Add, 2=Subtract, 3=Multiply, 4=Divide”)会显示重新生成消息。我错过了什么吗?
  • 我没有放try and catch方法。没关系。为什么你在 do 外面声明 int opInt = 0 而在里面声明 double 呢?
  • 我没有在while 中使用任何double 变量。你能详细说明你的最后评论吗?
  • 不,但我这样做是因为在私有静态 double multiplyValues 方法中使用了 double。感谢乔治的时间和精力。我在大学的时候做过德尔福。实际上,我在 Delphi 中用悖论完成了我的最后一个项目(我认为就是这样)。开发音乐商店。对比一下今天的申请,真是可笑的申请,哈哈,但是得到了很好的分数。
【解决方案3】:

如果您不更改 opInt 变量:

while(opInt < 1 || opInt > 4)
{
    switch (opInt)
    {
        case 1:
        break;
        case 2:
        break;
        default:
    }
}

【讨论】:

  • Tal 和 Quoi 都有效。使用 Quoi 我收到 opInt 的错误无法解析为变量,因为使用 Tal 没有任何反应。我认为它与私有静态 String getInput 方法有关,那么我可能又错了。
猜你喜欢
  • 1970-01-01
  • 2020-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-01
  • 1970-01-01
相关资源
最近更新 更多