使用 JOptionPane 将字符串数组转换为 int

Convert String Array to int using JOptionPane

我在做一个火车票程序。我想使用 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 总是 return 将用户的输入作为 String 对象,除了 return 所选 Object 的变体从提供的。为了示例,我正在谈论 Java 版本 8。

根据您的问题和评论,您基本上想要:

  1. String 个对象(它们的名称)与整数(它们的值)相匹配,这样您就可以计算出它们需要支付多少钱,加上之后的变化。
  2. 从用户那里接收他们将提供的金额作为输入,以计算零钱。

如果是,则:

  1. 对于第一种情况,您可以使用一些数据结构,从中您可以将位置与其值相匹配。例如 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 的 returned 值转换为 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 以外的其他对象类型,例如名为 Location 的 class 将具有 nameprice 作为属性,toString 方法将 return name.

  1. 对于第二部分,从 JOptionPane 获取用户输入作为 String 并对用户输入使用 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...");

记住,如果 parseInt 的输入 String 不是 int,它会抛出一个 NumberFormatException(这是一个 RuntimeException),所以你会必须使用 try-catch 块来检查。