如何正确调试程序以计算披萨的适当费用?

时间:2019-10-12 07:35:59

标签: java

总而言之,我的程序没有考虑到比萨饼中包含的每个附加配料,然后按预期将1.25加到基本成本上,然后适当地计算税款并最终产生总计。我的大脑迷糊了,试图弄清楚。有指针吗?

我为什至是一个潜在的解决方案而烦恼,所以我不确定该怎么做。

import java.util.Scanner;

public class PizzaOrder {
  public static void main(String[] args) {

//Declared variables
    String firstName; // First name of user
    char crustType; // Type of crust
    String crustName; // Name of crust
    int inches; // Pizza size
    final double Tax_Rate = 0.08; // Tax Rate
    double baseCost = 0.0; // Cost of pizza without tax and additional toppings.
    double total; // base cost plus the toppings
    double overallTotal; // Total cost including toppings and tax
    double tax; // Tax amount
    String toppings = "Cheese"; // Toppings list
    String choice;
    int numberOfToppings = 0; // Toppings counter

    // Create Scanner object
    Scanner keyboard = new Scanner(System.in);

    //Prompt and get the users first name
    System.out.println("Welcome to Mike and Diane's Pizza.");
    System.out.print("Enter your first name: ");
    firstName = keyboard.nextLine();


    //Prompt and get pizza size
    System.out.println("Pizza Size(inches)         Cost");
    System.out.println("      10                 $10.99    ");
    System.out.println("      12                 $12.99    ");
    System.out.println("      14                 $14.99    ");
    System.out.println("      16                 $16.99    ");
    System.out.print("Enter the size of the pizza you would like: ");
    inches = keyboard.nextInt();
    keyboard.nextLine(); //Consuming end of line character
    if(inches == 10) {
      baseCost = 10.99;
    }
    else if(inches == 12) {
      baseCost = 12.99;
    }
    else if(inches == 14) {
      baseCost = 14.99;
    }
    else if(inches == 16) {
      baseCost = 16.99;
    }
    else if(inches != 10 && inches != 12 && inches != 14 && inches != 16) {
      System.out.print("You have entered an invalid choice. The pizza size" +
      " will be set to 12 inches. ");
    }


    // Prompt and get crust choice
    System.out.print("Enter the type of crust you would like. " +
    " (H)and-Tossed, (T)hin-Crust, or (D)eep-Dish: ");
    crustName = keyboard.nextLine();
    crustType = crustName.charAt(0);

    // Output if the user selects Hand-Tossed
    if(crustType == 'H' || crustType == 'h') {
       crustName = "Hand-Tossed";
    System.out.println("You selected Hand-Tossed pizza. ");
    }

    // Output if the user selects Thin-Crust.
    else if(crustType == 'T' || crustType == 't') {
       crustName = "Thin-Crust";
    System.out.println("You selected Thin Crust pizza. ");
    }

    // Output if the user selects Deep-dish.
    else if(crustType == 'D' || crustType == 'd') {
       crustName = "Deep-dish";
    System.out.println("You selected Deep-Dish pizza. ");
    }

    // Otherwise print out the default pizza type.
    else {
      System.out.print("You have entered an invalid choice. The crust type" +
      "will be set to Hand-Tossed. ");
    }

    // Display message about toppings and additional cost
    System.out.println("All pizzas come with cheese.");
    System.out.println("Additional toppings are $1.25 each. Please choose" +
    " from Pepperoni, Onion, Sausage, and Mushroom. ");

    // Prompt and get topping choices one at a time. If topping is desired, add
    // name to topping list and increment number of toppings.
    System.out.print("Would you like pepperoni added? (Y/N): ");
    choice = keyboard.nextLine();
    if (choice.equals("Y") || choice.equals('y')) {
      numberOfToppings = numberOfToppings + 1;
      toppings = toppings + " and pepperoni.";
    }
    else {
      numberOfToppings = 0;
    }
    //Prompt for sausage and store in numberOfToppings
    System.out.print("Would you like sausage added? (Y/N): ");
    choice = keyboard.nextLine();
    if (choice.equals("Y") || choice.equals('y')) {
      numberOfToppings = numberOfToppings + 1;
      toppings = toppings + " and sausage.";
    }
    else {
      numberOfToppings = 0;
    }
    //prompt for onion and store in numberOfToppings
    System.out.print("Would you like onion added? (Y/N): ");
    choice = keyboard.nextLine();
    if (choice.equals("Y") || choice.equals('y')) {
      numberOfToppings = numberOfToppings + 1;
      toppings = toppings + " and onion.";
    }
    else {
      numberOfToppings = 0;
    }
    //prompt for mushroom and store in numberOfToppings
    System.out.print("Would you like mushroom added? (Y/N): ");
    choice = keyboard.nextLine();
    if (choice.equals("Y") || choice.equals('y')) {
      numberOfToppings = numberOfToppings + 1;
      toppings = toppings + " and mushroom.";
    }
    else {
      numberOfToppings = 0;
    }

    //Calculation of cost
    total = ((baseCost) + (numberOfToppings * 1.25));
    tax = total * Tax_Rate;
    overallTotal = total * (1 + Tax_Rate);

    // Payment Confirmation
    System.out.println(firstName + ", here is your order:");
    System.out.println(inches + " inch pizza");
    System.out.println(crustName + ", " + toppings);
    System.out.println("The cost of your order is: $" + (total));
    System.out.println("The tax is: $" + (tax));
    System.out.println("The overall total due is: $" + (overallTotal));
  }
}

预期输出将采用基本成本,将浇头的数量相加1.25倍,计算税额,然后将税额添加到包括浇头的成本中。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

您需要摆脱else块。代替:

if (choice.equals("Y") || choice.equals('y')) {
  numberOfToppings = numberOfToppings + 1;
  toppings = toppings + " and sausage.";
}
else {
  numberOfToppings = 0;
}

到处都是这样:

if (choice.equals("Y") || choice.equals('y')) {
  numberOfToppings = numberOfToppings + 1;
  toppings = toppings + " and sausage.";
}

如果用户在任何时候输入的不是“ Y”或“ y”,则浇头数量将重置为0。

我也不知道这行在做什么

overallTotal = total * (1 + Tax_Rate);

根据您的要求,逻辑应为:

overallTotal = total + tax;

您还可以使用Switch case语句代替IfElse阶梯。 希望这可以帮助!!!