java - 线程中出现异常,java代码将无法正常执行。 if/else 语句

标签 java eclipse if-statement

// Alex Deines
import javax.swing.*;  


public class DeinesAdmissions
{
    public static void main(String[] args)
    {
        String anwserString;
        double anwser;
        double y;

        anwserString= JOptionPane.showInputDialog(null,"Enter your GPA", "GPA", JOptionPane.QUESTION_MESSAGE);
        anwser = Double.parseDouble(anwserString);
            JOptionPane.showInputDialog(null,"Enter your Admission Test Score", "Test Score", JOptionPane.QUESTION_MESSAGE);
            y = Integer.parseInt(anwserString);

        if(anwser>=3 && y>=60 || anwser>=0 && y>=85 )
            JOptionPane.showMessageDialog(null, "You have been accepted");
        else 
            JOptionPane.showMessageDialog(null, "Sorry we must reject your application");


}

}

异常(exception):

 Exception in thread "main" java.lang.NumberFormatException: For input string: "3.2"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at DeinesAdmissions.main(DeinesAdmissions.java:16)

插入变量后,我收到此错误代码。有人能引导我走向正确的方向吗?

最佳答案

ydouble 所以使用 Double.parseDouble :(与anwser相同)

y = Double.parseDouble(anwserString);

您可以使用 Double.compare 比较 double

Compares the two specified double values. The sign of the integer value returned is the same as that of the integer that would be returned by the call:

new Double(d1).compareTo(new Double(d2))

注意拼写错误 anwser 应拼写为 answer,作者:@Katamari

您忘记在 @JohnnyMopp 的第二次调用中更新 anwserString

 anwserString = JOptionPane.showInputDialog(null,"Enter your Admission Test Score", "Test Score", JOptionPane.QUESTION_MESSAGE);

关于java - 线程中出现异常,java代码将无法正常执行。 if/else 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53764525/

相关文章:

java - 如何向实体添加方法(扩展)

eclipse - tomcat-maven-插件 : Server returned HTTP response code: 403

javascript - 如何为当前转换 : translate() value? 编写 if 表达式

java - UsbDevice requestPermission 被拒绝,且未显示请求对话框提示

java - 在 Java 中复制另一个数组的一部分

java - 在 Eclipse 编译期间在构建中嵌入 git-describe、日期和时间

PHP:如果 $_POST 中的数据与表中的数据匹配,则运行 mysql_query

javascript - jQuery - 如何检查一个元素是否存在?

java - 在 Java 中链接构造函数而不从默认构造函数抛出异常

eclipse - 在 Eclipse for Java 中将链式方法调用包装在单独的行上