gpt4 book ai didi

java - 如何处理NumberFormatException : null in Java to cancel and exit the program

转载 作者:行者123 更新时间:2023-11-30 08:05:04 25 4
gpt4 key购买 nike

我的代码中有这个。整个程序工作正常,除了当您从输入名称对话框中单击“取消”时,它会进入下一个对话框,要求输入回合数。最后退出。它应该在第一个对话框中退出。

enter image description here

有错误的代码:

    String iName = JOptionPane.showInputDialog (null, "Enter name:", JOptionPane.PLAIN_MESSAGE);
int iRounds = Integer.parseInt(JOptionPane.showInputDialog (null, "Enter number of rounds:", JOptionPane.PLAIN_MESSAGE));

enter image description here

当它应该立即退出时,单击取消会继续执行此操作:

enter image description here

知道如何修复此错误吗?谢谢!

最佳答案

String iName = JOptionPane.showInputDialog (null, "Enter name:", JOptionPane.PLAIN_MESSAGE);

if( ((iName != null) && (iName .length() > 0)) {
int iRounds = Integer.parseInt(JOptionPane.showInputDialog (null, "Enter number of rounds:", JOptionPane.PLAIN_MESSAGE));
}

关于java - 如何处理NumberFormatException : null in Java to cancel and exit the program,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31238517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com