有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

请求部分的java输出字

import javax.swing.*;
public class CarCareChoice2
{
    public static void main(String[] args)
    {
    final int NUM_OF_ITEMS = 8;
    String[] validChoices = {"oil change", "tire rotation", "battery check", "brake inspection"};
    int[] prices = {25, 22, 15, 5};
    String strOptions;
    String careChoice;
    double choicePrice = 0.0;
    boolean validChoice = false;
    strOptions = JOptionPane.showInputDialog(null, "Please enter one of the following care options: oil change, tire rotation, battery check, or brake inspection");
    careChoice = strOptions;
    for(int x = 0; x < NUM_OF_ITEMS; ++x)
    {
        if(careChoice.equals(validChoices[x]))
        {
            validChoice = true;
            choicePrice = prices[x];
        }
     }
     if(validChoice)
         JOptionPane.showMessageDialog(null, "The price of a(an) " + careChoice + " is $" + choicePrice);
     else
         JOptionPane.showMessageDialog(null, "Sorry - invalid entry");

    }
}

用户只需输入服务的前3个字母,即可获得他们输入的服务及其匹配价格。这是到目前为止我的代码


共 (1) 个答案

  1. # 1 楼答案

    User should only have to input the first 3 letters of the service and get the service they entered and its matching price

    这看起来不像是我会使用的方法,它不是用户友好的(IMHO),还有其他解决方法(按我认为最好的解决方法排序)

    1. 使用带有预定义选项的showInputDialog,看起来像JComboBox

      enter image description here

    2. 使用一个自定义的JPanel将作为消息传递,并使用JRadioButton以便用户可以选择其中一个(当然,您可以更改layout manager使其看起来不同

      enter image description here

    3. 最后是一种丑陋的方式,要求用户编写它,并使用^{}services变量和userInput变量进行比较,使用^{}方法(这是您试图实现的):

      enter image description here

    它们都产生了与此类似的输出:

    enter image description here

    注释/取消注释每个方法以查看它们的行为,并相应地选择生成上述每个输出的代码:

    import java.awt.Component;
    
    import javax.swing.ButtonGroup;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.SwingUtilities;
    
    public class CarCareChoice {
        
        private JFrame frame;
        private String[] services = {"Oil change", "Tire rotation", "Battery check", "Brake inspection"};
        
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    CarCareChoice carCareChoice = new CarCareChoice();
    //              carCareChoice.comboWay();
    //              carCareChoice.radioWay();
                    carCareChoice.textUglyWay();
                }
            });
        }
        
        public void comboWay() {
            frame = new JFrame();
            
            String userSelection = (String) JOptionPane.showInputDialog(frame, "Please select one of the following", "Service selection", 
                    JOptionPane.PLAIN_MESSAGE, null, services, services[0]);
            
            if (userSelection != null) {
                JOptionPane.showMessageDialog(frame, "The service selected was: " + userSelection);
            }
        }
        
        public void radioWay() {
            frame = new JFrame();
            JPanel pane = new JPanel();
            JRadioButton[] buttons = new JRadioButton[services.length];
            ButtonGroup group = new ButtonGroup();
            
            for (int i = 0; i < buttons.length; i++) {
                buttons[i] = new JRadioButton(services[i]);
                group.add(buttons[i]);
                pane.add(buttons[i]);
            }
            
            int option = JOptionPane.showConfirmDialog(frame, pane, "Service selection", JOptionPane.PLAIN_MESSAGE, JOptionPane.YES_NO_OPTION);
            
            JRadioButton userSelection = null;
            
            if (option == JOptionPane.YES_OPTION) {
                for (Component c : pane.getComponents()) {
                    if (c instanceof JRadioButton) {
                        if (((JRadioButton) c).isSelected()) {
                            userSelection = (JRadioButton) c;
                            break;
                        }
                    }
                }
                if (userSelection != null) {
                    JOptionPane.showMessageDialog(frame, "Service selected: " + userSelection.getText());
                } else {
                    JOptionPane.showMessageDialog(frame, "C'mon man! Select something");
                }
            }
        }
        
        public void textUglyWay() {
            frame = new JFrame();
            
            String userSelection = (String) JOptionPane.showInputDialog(frame, "Please select one of the following", "Service selection", 
                    JOptionPane.PLAIN_MESSAGE);
            
            if (userSelection != null && !userSelection.equals("")) {
                for (String s : services) {
                    if (s.toLowerCase().startsWith(userSelection.toLowerCase())) {
                        userSelection = s;
                        break;
                    }
                }
                JOptionPane.showMessageDialog(frame, "The service selected was: " + userSelection);
            }
        }
    }
    

    重要提示:

    您的程序也没有放在Event Dispatch Thread (EDT)上,这可能会在将来导致一些线程问题,因此请确保将您的所有程序放在其中,就像我放在Event Dispatch Thread (EDT)上一样:

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                //Your constructor here
            }
        });
    }