gpt4 book ai didi

java - 如果语句没有运行

转载 作者:行者123 更新时间:2023-12-01 17:05:12 25 4
gpt4 key购买 nike

字符串被解析为整数,因此 if 语句的条件设置正确。为什么 if 语句没有运行?为什么没有出现带有响应的 MessageDialog?

 class process{
public static void whoIs(){

JFrame frame=new JFrame("The Oldest");
String a=JOptionPane.showInputDialog(frame, "Enter, please, the first name and age:", "QUIZ: Who is the Oldest", JOptionPane.QUESTION_MESSAGE);
String b=JOptionPane.showInputDialog(frame, "Enter, please, the second name and age:", "QUIZ: Who is the Oldest", JOptionPane.QUESTION_MESSAGE);


String age1=a.replaceAll("[^\\d]","");
String age2=a.replaceAll("[^\\d]","");



String name1=a.replaceAll("\\d","");
String name2=b.replaceAll("\\d","");


int age1int=Integer.parseInt(age1);
int age2int=Integer.parseInt(age2);



if (age1int>age2int){
JOptionPane.showMessageDialog(frame, name1+ " is the oldest!", "QUIZ: Who is the Oldest?", JOptionPane.INFORMATION_MESSAGE);
}

if (age2int>age1int) {
JOptionPane.showMessageDialog(frame, name2+ " is the oldest!", "QUIZ: Who is the Oldest?", JOptionPane.INFORMATION_MESSAGE);
}

}
}

最佳答案

您的两个年龄相同,并且您的 if 条件不匹配,因为您没有考虑平等。我认为您错过了这一点,下次尝试使用调试器,然后您可以自己识别此类问题。

 String age1=a.replaceAll("[^\\d]","");
String age2=a.replaceAll("[^\\d]","");

这应该将其更改为以下。

  String age1=a.replaceAll("[^\\d]","");
String age2=b.replaceAll("[^\\d]","");

关于java - 如果语句没有运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25931229/

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