首页 > 解决方案 > 使用 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);
    }

- 我变成评论的代码给了我错误

标签: javaeclipseswingjoptionpane

解决方案


showInputDialog始终将用户的输入作为String对象返回,除了从提供的返回中选择的变体Object。为了举例,我正在谈论 Java 版本 8。

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

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

如果是这样,那么:

  1. 对于第一种情况,您可以使用一些数据结构,您可以从中将位置与其值匹配。例如 aHashMap<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...");

或者更好的是,您可以使用Stringoptions 数组以外的其他对象类型,例如一个名为的类Location,它将具有 thenamepriceas 属性,其toString方法将返回name.

  1. 对于第二部分,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 ) ,因此您必须使用-块检查它。RuntimeExceptionStringinttrycatch


推荐阅读