如何使用if else语句和对话框?

时间:2017-04-10 23:59:49

标签: java ios eclipse if-statement dialog

我有一份学校作业,需要我为剧院制作票务系统。所以条件是周一至周四,10岁以下的人免费,10-65人必须支付10令吉,65岁以上的人也可以免费。周五至周日,10岁以下的人必须支付10令吉,10-65支付20令吉,65岁以上支付10令吉。

我尝试放入对话框,到目前为止我得到了这个:

import javax.swing.*;
public class NewTicketSystem {

public static final String [] days = { "Monday", "Tuesday", "Wednesday", "Thursday",
        "Friday", "Saturday", "Sunday"};

public static void main(String[] args) {

    JFrame frame = new JFrame("Input Dialog Example 3");
    String Day = (String) JOptionPane.showInputDialog(frame, 
        "Which day do you want to choose?",
        "Day",
        JOptionPane.QUESTION_MESSAGE, 
        null, 
        days, 
        days[0]);

    System.out.printf("Chosen day is %s.\n", Day);

    JFrame frames = new JFrame("InputDialog Example #1");


    String UserAge = JOptionPane.showInputDialog(frames, "What is your age?");

    System.out.printf("Your age is '%s'.\n", UserAge);

     System.exit(0);


    }

}

我现在的问题是,如何将条件纳入编码?我尝试过在线搜索,但仍然无法理解。如果可能,请用简单的语言解释。

提前谢谢。

1 个答案:

答案 0 :(得分:1)

int price;

if (Day.equals("Monday") || Day.equals("Tuesday") || Day.equals("Wednesday") || Day.equals("Thursday")) { //Monday - Thursday
    if (UserAge >= 10 && UserAge <=65) //10-65
        price = 10;
    else //Under 10 and Above 65
        price = 0;
} else { //Friday - Sunday
     if (UserAge >= 10 && UserAge <=65) //10-65
         price = 20;
     else //Under 10 and Above 65
         price = 10;
}