【问题标题】:Java: Accessing A Variable Inside a Try BlockJava:访问 Try 块中的变量
【发布时间】:2016-02-17 00:26:43
【问题描述】:

我知道这个问题已经被问过几次,我已经尝试了回复中的建议,但它不适用于我的特定情况。

这是一个学校作业,仅供参考。

我正在编写一个简单的方法来检查用户是否使用 try/catch 块输入了数值。问题是,我的教授对未使用的变量进行了评分,这是有道理的,我想不出任何使用 userInputValue 变量的方法。我尝试在 MessageDialog 中使用它,但由于它是在 Try 块中声明的,因此无法访问它。我尝试将其移出块外,但随后该变量未使用。有什么办法可以改写它,但保留相同的功能,而没有那个未使用的变量?

public boolean numericalUserInput(String userInput){

    try {
        double userInputValue = Double.parseDouble(userInput);
    }
    catch(NumberFormatException notANumber){
        JOptionPane.showMessageDialog(null, "You entered " + userInput + " but you should only enter numbers, please try again.");
        userEntryTextField.setText("");
        return false;
    }
    return true;
}

谢谢!

【问题讨论】:

  • 为什么有变量?只需致电Double.parseDouble(userInput); 即可完成。
  • 把你的代码放在你想使用的地方 userInputValue 在 try 块内
  • @SteffenKreutz 我不知道为什么,但它以前不是这样工作的,我一定同时重新格式化了一些其他代码。它现在是这样工作的。谢谢!
  • @azurefrog 以前由于某种原因不能这样工作,但现在可以了。我一定改变了我正在玩的其他代码。现在可以使用了,谢谢!

标签: java exception-handling try-catch


【解决方案1】:

在你的方法中你不需要一个变量来携带 parse double 方法的返回值

public boolean numericalUserInput(String userInput){

try {
    Double.parseDouble(userInput);
}
catch(NumberFormatException notANumber){
    JOptionPane.showMessageDialog(null, "You entered " + userInput + " but you should only enter numbers, please try again.");
    userEntryTextField.setText("");
    return false;
}
return true;
}

如果你需要返回的号码,你可以改变你的方法来使用这个号码,如下所示

public double numericalUserInput(String userInput){
     double userInputValue;
try {
    userInputValue = Double.parseDouble(userInput);
}
catch(NumberFormatException notANumber){
    JOptionPane.showMessageDialog(null, "You entered " + userInput + " but you should only enter numbers, please try again.");
    userEntryTextField.setText("");
    return Double.NaN;
}
return userInputValue ;
}

【讨论】:

    【解决方案2】:

    您似乎不需要使用 userInputValue ,因为您只是使用此方法来检查 userInput 字符串是否为数字。您可以像这样离开 userInputValue:

    public boolean numericalUserInput(String userInput){
    
        try {
            Double.parseDouble(userInput);
        }
        catch(NumberFormatException notANumber){
            JOptionPane.showMessageDialog(null, "You entered " + userInput + " but you should only enter numbers, please try again.");
            userEntryTextField.setText("");
            return false;
        }
        return true;
    }
    

    【讨论】:

      【解决方案3】:

      由于你不需要解析后的数字,你可以简单地省略赋值:

      public boolean numericalUserInput(String userInput){
          try {
              Double.parseDouble(userInput);
          } catch(NumberFormatException notANumber){
              JOptionPane.showMessageDialog(null, "You entered " + userInput + " but you should only enter numbers, please try again.");
              userEntryTextField.setText("");
              return false;
          }
      
          return true;
      }
      

      【讨论】:

        猜你喜欢
        • 2017-11-02
        • 2011-02-20
        • 2018-08-27
        • 2012-05-13
        • 1970-01-01
        • 2013-02-08
        • 1970-01-01
        • 1970-01-01
        • 2014-06-23
        相关资源
        最近更新 更多