java - 没有错误,但应显示订购的披萨类型和数量的部分代码无法正常工作

标签 java arrays loops

public class PizzaExamfinalOne{
  public static void main(String[]args){
    double total2 = 0;
    String customerName = " ";
    String address = " ";
    String phoneNum ="0";
    int max = 5;
    int done = 1;
    double totalPizza = 0;
    double totalA = 0;

    int pizzaChoice =0;

    String [] pizzaType = {"Placeholder",  "Hawaian" , "Classic Italian" , "Cheese Extreme" , "Veg Delight" , "Pepperoni" , "Tomato Margherita" , "Beef & Onion" , "Super Supreme" , "Meat Lovers" , "Hot 'n Spicy" , "Italiano" , "Italian Veg"};
    double [] pizzaPrice = {0, 8.5 , 8.5 , 8.5 , 8.5 , 8.5 , 8.5 , 8.5 , 13.5 , 13.5 , 13.5 , 13.5 , 13.5};
    int [] orderDisplay = {0,0,0,0,0,0,0,0,0,0,0,0,0};

    boolean pickup = true;
    int pickup1 = readInt("If you wish to pickup press 1 or  press 0 for delivery");
    if (pickup1 ==0){
      pickup = false;
    }

    for (int i = 1; i <pizzaType.length; i++){
      System.out.println(i + " " +pizzaType[i] + " $" + pizzaPrice[i]);
    }

    while (done !=0){

      pizzaChoice = readInt ("Choose the type of pizza you wish to order");
      double pizzaQuantity = readInt ("How many " + pizzaType[pizzaChoice] + " pizzas do you wish to buy (Maximum is 5)");   

      totalPizza = totalPizza + pizzaQuantity;
      if(totalPizza > max){
        System.out.println("You cannot order more than five pizzas");
        pizzaChoice = 0;  
        pizzaQuantity = 0;
      }

      done= readInt ("Press 0 if you are done or press 1 to make another pizza order");
      double total1 = pizzaQuantity*pizzaPrice[pizzaChoice];
      total2 = total2 + total1;

    }

    if(pickup==false){
      int deliveryCost = 3;
      customerName = readString ("Please enter your full name");
      address = readString ("Please enter the Delivery address");
      phoneNum = readString ("Please enter the phone number of " + customerName);
      totalA = total2 + deliveryCost;
      System.out.println("Delivery order for " + customerName);
      System.out.println("To " + address + ", Phone number 0" + phoneNum);

    }
      else{
      customerName = readString ("Please enter your full name");
      System.out.println("Pickup order for " + customerName);
      int deliveryCost = 0;
      totalA = total2 + deliveryCost;

    }

我的问题就在这里。我需要显示用户订购的披萨以及订购的披萨数量,但我被困在这里。代码中没有错误,只是不显示以下内容

    for(int i=1;i<orderDisplay.length;i++){
      if(orderDisplay[i]>0){
        System.out.println("       " + orderDisplay[i] +  " " + pizzaType[i] + " pizzas");
      }
    }
    System.out.println("The total amount of your order is " + formatMoney(totalA));
  }


  public static int readInt(String prompt){
    System.out.println(prompt);
    java.util.Scanner keyboard = new java.util.Scanner(System.in);                                
    return keyboard.nextInt();
  }
  public static String readString (String prompt){
    System.out.println (prompt);
    java.util.Scanner keyboard= new java.util.Scanner(System.in);
    return keyboard.nextLine();
  }

  public static String formatMoney(double phoneNum){
    java.text.NumberFormat  fmt = java.text.NumberFormat. getCurrencyInstance();
    return(fmt.format(phoneNum));
  }
}

最佳答案

您不向 orderDisplay 数组添加订单。

您应该在 if(pickup==false){..} 中处理它

关于java - 没有错误,但应显示订购的披萨类型和数量的部分代码无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10596964/

相关文章:

Java 返回一个新对象 *完全初学者

java - eclipse java 注释处理提交生成的带有 subclipse 的 java 类到 svn

java - 什么是语法或格式不严格的良好配置格式?

java - 如何在 Java 中检索词素的所有变体?

javascript - JS : Associative array access over variable?

java - 使用重复键快速排序会变得更快(没有三向分区)。到底是怎么回事?

python - For 循环运行直到达到变量值

python - 视频播放速度慢 - Python 3.6、OpenCV 3、PyQT 5

python - 打破两个for循环

c - 将数据存储在头文件中包含数组的 Stucts 中