2017-02-22 54 views
-2
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個字母並獲得他們輸入的服務及其匹配的價格。這是我的代碼到目前爲止。部分請求的輸出字

+2

你的問題是什麼 –

+0

用戶只需要輸入服務的前3個字母,並獲得他們輸入的服務及其匹配的價格。 – Jokes

+0

你無法解釋你的問題 –

回答

0

用戶應該只需要輸入服務的前3個字母,讓他們進入了服務及其配套價格

這不會看起來像一個方法,我會用,這不是用戶友好(恕我直言),還有其他的方法來解決這個問題(在我是怎麼想就解決它的最好訂購)

  1. 使用預定義的選項showInputDialog,它會看起來像一個JComboBox

    enter image description here

  2. 使用自定義JPanel這將作爲消息傳遞和使用JRadioButton如此的用戶可以選擇其中之一(當然你可以改變layout manager看起來不同

    enter image description here

  3. 最後是醜陋的方法,要求用戶編寫它,並在services變量和userInput變量上使用.toLowerCase()進行比較,使用.startsWith()方法(這是你想要達到的目的):

    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)這可能會導致一些線程問題在未來,所以一定要所有的程序放置在它爲我做的事:

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