提示用户是希望应用搜索整数还是字符串



如何提示用户他们是否希望应用程序使用 Joption 搜索整数或字符串并切换大小写语句...我对如何编码感到非常迷茫

Object[] options = {"Integer Value", "String Value"};
int choise = JOptionPane.showOptionDialog(null, "What do you want to search for?", "Box Title",
JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
switch ((String) options[choise]) {
case "Integer Value":
System.out.println("They chose an integer!");
break;
case "String Value":
System.out.println("They chose a string!");
break;

第 1 步:创建一个所有可能选项的对象 []。在这种情况下,它们是"整数值"和"字符串值"。

第 2 步:调用 JOptionPane 类。我们在父选项(null(、问题、窗口标题、选项类型、选项上想要的图像、图标(null(、选项和默认选项(选项[0](中解析。此方法返回一个整数,该整数是用户选择的整数。

第 3 步:使用开关盒中的选项执行任何您想要的操作。您不需要使用字符串的开关大小写,它可以只使用整数,但我认为这样更清晰。

最新更新