我一直在四处寻找,但很难找到这个特定代码的答案。我希望让 if 语句搜索分隔符 - 然后使用将之前和之后的整数应用于变量。例如。如果他们输入 1-50,则会将 1 应用于 int a,将 50 应用于 int b。这是我想要应用它的 if 语句。它将来自输入变量:
if (yes.equalsIgnoreCase("no")){
if (a.equalsIgnoreCase(answer) && input.equalsIgnoreCase("instant")){
Thread.sleep(1);
}
else if (a.equalsIgnoreCase(answer) && input.equalsIgnoreCase("generate")){
Thread.sleep(randomInt);
}
else if (a.equalsIgnoreCase(answer) && !input.equalsIgnoreCase("generate") && !input.equalsIgnoreCase("instant")){
int x =Integer.parseInt(input);
x = x*60000;
Thread.sleep(x);
}
else if (a.equals("quit" )){
System.exit(0);
}
else if (a.equals("Quit")){
System.exit(0);
}
else if (a.equals("")){
}
else{
JOptionPane.showMessageDialog(null,"Please check the answer then try again ");
i = i-2;
}
感谢您的帮助。
最佳答案
看看String.split()
。它将返回一个 String[]
,稍后您可以使用 Integer.valueOf()
对其进行解析。
例如:
String s = "1-50";
System.out.println(Arrays.toString(s.split("-")));
将打印[1,50]
关于java - 如何让 if 语句搜索分隔符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9401486/