在 JOptionPane 中区分用户的输入
differentiating inputs by user in JOptionPane
我想编写一个程序,向用户显示一个框并要求输入名称。
如果姓名输入正确(真实姓名),最终消息会出现,但如果用户输入整数,程序会要求用户再次输入字符串中的真实姓名。
代码:
import javax.swing.*;
public class Project018 {
public static void main(String[] args) {
String name = JOptionPane.showInputDialog("What is your name?");
try {
int number = Integer.parseInt(name);
} catch (NumberFormatException n){
JOptionPane.showMessageDialog(null, "Dear " + name + "\nwelcome to java programming course");
}
String message = String.format("your name must not contain any number");
JOptionPane.showMessageDialog(null, message);
}
}
我想知道如何在用户输入整数时将程序循环回到顶部,以及如何在输入真实姓名时跳过第二条消息
I want to know how I can loop back the program to the top when the user types an integer
嗯,为此我会使用 do-while
循环。
为了检查输入是否有数字,您不应该尝试将数字解析为整数。相反,我会使用带有 Matcher
和 regex
的 Pattern
。否则你不会考虑这种情况:Foo123
在这种情况下,类似于:[0-9]+
用于正则表达式,如果 Matcher
与其匹配,则它有一个数字。
how I can skip the second message when a real name is entered
基于 Matcher
是否与您在一个对话框或另一个对话框中显示的内容相匹配
例如:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
public class LoopingJOptionPane {
private static final String REGEX = "[0-9]+";
public static void main(String[] args) {
SwingUtilities.invokeLater(new LoopingJOptionPane()::createAndShowGui);
}
private void createAndShowGui() {
boolean found = true;
do { //This repeats this code if input is incorrect
Pattern pattern = Pattern.compile(REGEX);
String name = JOptionPane.showInputDialog("Enter your name");
Matcher matcher = pattern.matcher(name); //We try to find if there's a number in our string
found = matcher.find();
if (found) { //If there's a number, show this message
JOptionPane.showMessageDialog(null, "Please write only letters");
} else { //Otherwise show this one
JOptionPane.showMessageDialog(null, "Welcome " + name);
}
} while (found); //If it has no numbers, don't repeat
}
}
我想编写一个程序,向用户显示一个框并要求输入名称。
如果姓名输入正确(真实姓名),最终消息会出现,但如果用户输入整数,程序会要求用户再次输入字符串中的真实姓名。
代码:
import javax.swing.*;
public class Project018 {
public static void main(String[] args) {
String name = JOptionPane.showInputDialog("What is your name?");
try {
int number = Integer.parseInt(name);
} catch (NumberFormatException n){
JOptionPane.showMessageDialog(null, "Dear " + name + "\nwelcome to java programming course");
}
String message = String.format("your name must not contain any number");
JOptionPane.showMessageDialog(null, message);
}
}
我想知道如何在用户输入整数时将程序循环回到顶部,以及如何在输入真实姓名时跳过第二条消息
I want to know how I can loop back the program to the top when the user types an integer
嗯,为此我会使用 do-while
循环。
为了检查输入是否有数字,您不应该尝试将数字解析为整数。相反,我会使用带有 Matcher
和 regex
的 Pattern
。否则你不会考虑这种情况:Foo123
在这种情况下,类似于:[0-9]+
用于正则表达式,如果 Matcher
与其匹配,则它有一个数字。
how I can skip the second message when a real name is entered
基于 Matcher
是否与您在一个对话框或另一个对话框中显示的内容相匹配
例如:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
public class LoopingJOptionPane {
private static final String REGEX = "[0-9]+";
public static void main(String[] args) {
SwingUtilities.invokeLater(new LoopingJOptionPane()::createAndShowGui);
}
private void createAndShowGui() {
boolean found = true;
do { //This repeats this code if input is incorrect
Pattern pattern = Pattern.compile(REGEX);
String name = JOptionPane.showInputDialog("Enter your name");
Matcher matcher = pattern.matcher(name); //We try to find if there's a number in our string
found = matcher.find();
if (found) { //If there's a number, show this message
JOptionPane.showMessageDialog(null, "Please write only letters");
} else { //Otherwise show this one
JOptionPane.showMessageDialog(null, "Welcome " + name);
}
} while (found); //If it has no numbers, don't repeat
}
}