java - 使用 JOptionPane 将字符串数组转换为 int
问题描述
我正在做一个火车票计划。我想使用 JOptionPane 将字符串数组中的某些文本转换为某个价格。
前任。
String[] option_1 = {"location1","location2", "location3","location4","location5"};
其中位置 1 = 10 美元,位置 2 = 23 美元,以此类推。
我只知道我可以使用 Interger.parseInt 或 double,但我不知道下一步该去哪里。我应该使用循环还是创建一个全新的数组并使其等于字符串数组。我想知道是否有更简单的方法来执行此操作。
代码 :
public static void main (String [] args) {
String name;
String[] option_1 = {"North Avenue","Quezon Avenue", "Kamuning","Cubao","Santolan","Ortigas","Shaw","Boni","Guadalupe","Buendia","Ayala","Magallanes","Taft"};
String[] option_2 = {"North Avenue","Quezon Avenue", "Kamuning","Cubao","Santolan","Ortigas","Shaw","Boni","Guadalupe","Buendia","Ayala","Magallanes","Taft"};
name = JOptionPane.showInputDialog("Welcome to DME Ticketing System!\n\nEnter your name:");
String leave = (String)JOptionPane.showInputDialog(null, "Leaving from",
"Train Station", JOptionPane.QUESTION_MESSAGE, null, option_1, option_1[0]);
String going = (String)JOptionPane.showInputDialog(null, "Leaving from",
"Train Station", JOptionPane.QUESTION_MESSAGE, null, option_2, option_2[0]);
// int pay = (Integer)JOptionPane.showInputDialog(null, "From: "+leave+"\nTo: "+going+"\nFare Price: "+"\n\nEnter your payment amount:",
// null, JOptionPane.PLAIN_MESSAGE, null, null,null);
// int op1 = Integer.parseInt(going);
// int op2 = Integer.parseInt(leave);
JOptionPane.showMessageDialog(null, "DME Ticketing System\nMalolos, Bulacan\n\n"
+ "Name: "+name
+"\nLocation: "+leave
+"\nDestination: "+going
+"\nFare: "
+"\nPayment: "//+pay
+"\nChange: "
, "RECEIPT",JOptionPane.INFORMATION_MESSAGE);
}
- 我变成评论的代码给了我错误
解决方案
showInputDialog
始终将用户的输入作为String
对象返回,除了从提供的返回中选择的变体Object
。为了举例,我正在谈论 Java 版本 8。
根据您的问题和评论,您基本上想要:
- 将对象(它们的名称)与整数(它们的值)相匹配
String
,这样您就可以计算出它们需要支付的金额,以及之后的零钱。 - 从用户那里接收他们将给予的金额作为输入,以计算变化。
如果是这样,那么:
- 对于第一种情况,您可以使用一些数据结构,您可以从中将位置与其值匹配。例如 a
HashMap<String, Integer>
可以将位置与其值匹配,如下所示:
//Set up the origin options, along with their value:
HashMap<String, Integer> leavingOptions = new HashMap<>();
leavingOptions.put("North Avenue", 10);
leavingOptions.put("Quezon Avenue", 11);
leavingOptions.put("Kamuning", 12);
leavingOptions.put("Cubao", 13);
leavingOptions.put("Santolan", 14);
leavingOptions.put("Ortigas", 15);
leavingOptions.put("Shaw", 16);
leavingOptions.put("Boni", 17);
leavingOptions.put("Guadalupe", 18);
leavingOptions.put("Buendia", 19);
leavingOptions.put("Ayala", 20);
leavingOptions.put("Magallanes", 21);
leavingOptions.put("Taft", 22);
//Get user input for origin:
Object[] leavingOptionsArray = leavingOptions.keySet().toArray();
String leaving = (String) JOptionPane.showInputDialog(null, "Leaving from:", "Train Station", JOptionPane.QUESTION_MESSAGE, null, leavingOptionsArray, leavingOptionsArray[0]);
//Show output depending on user's input or cancel:
if (leaving != null)
JOptionPane.showMessageDialog(null, "You shall pay: " + leavingOptions.get(leaving));
else
JOptionPane.showMessageDialog(null, "Quiting...");
(请注意,将返回值showInputDialog
转换为String
引用是安全的,因为我们将选项数组设置为仅包含String
对象的方式)
或者,您可以使用String
具有位置的int
数组以及具有相应值的数组,如下所示:
//Set up the origin options, along with their price:
String[] leavingOptions2 = new String[]{"North Avenue","Quezon Avenue", "Kamuning","Cubao","Santolan","Ortigas","Shaw","Boni","Guadalupe","Buendia","Ayala","Magallanes","Taft"};
int[] price = new int[]{10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22};
//Get user input for origin:
Object leaving2 = JOptionPane.showInputDialog(null, "Leaving from:", "Train Station", JOptionPane.QUESTION_MESSAGE, null, leavingOptions2, leavingOptions2[0]);
//Show output depending on user's input or cancel:
if (leaving2 != null) {
//Find the price of the selected location:
int p = -1;
for (int i = 0; i < price.length; ++i)
if (leaving2.equals(leavingOptions2[i])) {
p = price[i];
break;
}
JOptionPane.showMessageDialog(null, "You shall pay: " + p);
}
else
JOptionPane.showMessageDialog(null, "Quiting...");
或者更好的是,您可以使用String
options 数组以外的其他对象类型,例如一个名为的类Location
,它将具有 thename
和price
as 属性,其toString
方法将返回name
.
- 对于第二部分,
String
从 a中获取用户的输入,JOptionPane
并在用户的输入上使用Integer.parseInt(...)
方法,以便之后能够计算变化,如下所示:
String paymentString = JOptionPane.showInputDialog(null, "Enter payment amount:", "Payment", JOptionPane.QUESTION_MESSAGE);
if (paymentString != null) {
try {
int payment = Integer.parseInt(paymentString);
JOptionPane.showMessageDialog(null, "You chose to pay: " + payment);
//Calculate change here...
}
catch (final NumberFormatException exception) {
JOptionPane.showMessageDialog(null, "The input was invalid (not an 'int').");
}
}
else
JOptionPane.showMessageDialog(null, "Quiting...");
请记住,如果它的输入不是a ,parseInt
它将抛出 a NumberFormatException
(这是 a ) ,因此您必须使用-块检查它。RuntimeException
String
int
try
catch
推荐阅读
- docker - 官方 Docker 镜像说 docker 没有运行?
- leaflet - 如何填充瓷砖传单的颜色?
- reactjs - 酶测试代码内存清理
- javascript - 在这个 ES6 示例中,“静态”是什么意思?
- python - 如何从单词列表中选择python中的随机单词
- c# - 与剃须刀页面相比,.net core 2.1 mvc 中的 csvhelper 工作方式是否不同?
- vba - 您将如何单击带有 Visual Basic 的“我不是机器人”复选框?
- sql - 在使用使用 Unix 时间戳的自定义 SQL 查询参数时,我可以使用日历日期选择器吗?
- parse-platform - 解析云函数响应错误
- amazon-web-services - 将不同的排序键响应映射到 Appsync 架构值