Java Ticket价格计算器if语句

时间:2016-09-19 02:44:49

标签: java

好的,所以我非常感谢这个社区的建议。我最近收到了我的第一个编程项目,而且我已经开始了。我没有编程经验,但我会尽可能多地提供信息。

该项目是一个基本的票证定价程序,它使用java在命令提示符下运行。我们必须介绍具有创建者名称的程序,询问想要的成人票数(每件10美元),学生票数(每件5美元),使用switch语句或if语句以允许用户选择折扣(免运费,10%折扣或最佳选择),最后根据用户的选择计算总数。

问题是我只是停留在整个switch语句中并计算总数。我将包含我到目前为止的代码,我非常感谢你能给予的任何帮助。

import java.util.Scanner;

public class Project1
{
    public static void main(String[] args)
{
    Scanner kb = new Scanner(System.in);
    double adult; //number of adult tickets
    double student; //number of student/senior tickets
    int option;
    double aamount = 10.00;
    double samount = 5.00;
    double purchasePrice = adult * aamount + student * samount;
    double total;

    final double DISCOUNT = 0.1;
    final double SHIPPING = 5.00;

    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println("******** WELCOME TO THE TICKET PRICE CALCULATOR ********");
    System.out.println("            created by Shane Slobecheski");
    System.out.println();
    System.out.println("               Press enter to continue");
    kb.nextLine();
    System.out.println();
    System.out.print("How many adult tickets do you wish to purchase? (ages 18-64): ");
    adult = kb.nextInt();
    System.out.println();
    System.out.print("How many student/senior tickets do you wish to purchase? (ages 0-17 or 65+): ");
    student = kb.nextInt();
    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println("Please make a selection from the menu options below for additional savings");
    System.out.println();
    System.out.println("  Savings Menu");
    System.out.println("-----------------");
    System.out.println("1. Free Shipping");
    System.out.println("2. 10.0% discount");
    System.out.println("3. Apply whichever discount will save me the most money");
    System.out.println();
    System.out.print("Enter your selection: ");
    option = kb.nextInt();
    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println ("Purchase price $"+ purchasePrice);

    purchasePrice = adult * aamount + student * samount;

}
}//end program

1 个答案:

答案 0 :(得分:0)

不要忘记他们的第一个初始化变量

double adult=0; 
double student=0; 
int option=0;
double total=0;

并且switch结构具有以下语法,其中p是用于评估其严重案例选项的变量

   switch(op)
    {
        case 1:operations; break;
        case 2: operations;break;
        default: System.out.println("option no valid");
    }