【发布时间】:2021-09-13 17:23:08
【问题描述】:
我正在做一个火车票项目。我想使用 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);
}
-我变成 cmets 的代码给了我错误
【问题讨论】:
-
我相信这会有所帮助:Creating a GUI With JFC/Swing
-
用户输入支付金额了吗?我问这个是因为在您的 cmets 中似乎有一个
JOptionPane要求用户付款。另一方面,如果您尝试将位置 (Strings) 映射到它们的值 (ints),那么HashMap<String,Integer>就足够了(或者可能是一些数组或列表)。 -
是的,用户将输入付款金额,因为我将计算收据中的变化。谢谢,我会尝试这些方法。
标签: java eclipse swing joptionpane