如果用户输入与正确的格式不匹配,我试图让这个程序重启。但是我不确定如何做到这一点。我附上了以下代码:
package weeek4;
import javax.swing.JOptionPane;
public class rollNumber {
public static void main(String[] args) {
String input = JOptionPane.showInputDialog(null,"Enter your Roll Number in this format **-***-*****");
if (input.matches("\\d{2}-\\d{3}-\\d{5}")) {
JOptionPane.showMessageDialog(null,"Thank you");
}
else {
JOptionPane.showMessageDialog(null,"Invalid Number");
}
}
}
答案 0 :(得分:1)
这很容易。只需使用loop
(在满足某个条件之前重复代码块的语句)和一个标志变量(在这种情况下,k
检查while
循环中的条件)
package weeek4;
import javax.swing.JOptionPane;
public class rollNumber
{
public static void main(String[] args)
{
String input = JOptionPane.showInputDialog(null,"Enter your Roll Number in this format **-***-*****");
int k = 0;
while (k!=1)
{
if (input.matches("\\d{2}-\\d{3}-\\d{5}")) {
JOptionPane.showMessageDialog(null,"Thank you");
k=1;
}
else
JOptionPane.showMessageDialog(null,"Invalid Number");
}
}
}
希望我帮助过:)