gpt4 book ai didi

java - 提示用户是否希望应用程序搜索整数或字符串

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

如何提示用户是否希望应用程序使用 Joption 和 switch case 语句搜索整数或字符串...我不知道如何编写此代码

最佳答案

    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)、我们的选项和默认选项(options[0])中进行解析。此方法返回一个用户选择的整数。

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

关于java - 提示用户是否希望应用程序搜索整数或字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58227954/

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