我的java程序只提示用户,不读取数据。如何让我的代码实际读取数据并执行计算?

我的java程序只提示用户,不读取数据。如何让我的代码实际读取数据并执行计算?,java,Java,我的程序应该询问用户是否想要订购比萨饼,用户应该输入 是或否(全部小写) 输入“否”将退出程序,但输入“是”将使计算机再次提示用户输入此信息- 姓氏(一个字,无需验证) 比萨饼类型的选择(选项包括素食、奶酪、辣香肠和supreme) 比萨饼大小的选择(选项有小、中、大) 输入所有这些信息后,程序应打印以下信息 客户姓氏(无论输入什么) 比萨饼的成本 大型比萨饼的总数 中型比萨饼的总数 小比萨饼的总数 订单的平均成本 当我编译我的程序时没有错误,当我运行它时,我得到了完美的提示……然而,在你输入答

我的程序应该询问用户是否想要订购比萨饼,用户应该输入

是或否
(全部小写)

输入“否”将退出程序,但输入“是”将使计算机再次提示用户输入此信息-

姓氏
(一个字,无需验证)

比萨饼类型的选择
(选项包括素食、奶酪、辣香肠和supreme)

比萨饼大小的选择
(选项有小、中、大)

输入所有这些信息后,程序应打印以下信息

客户姓氏
(无论输入什么)

比萨饼的成本

大型比萨饼的总数

中型比萨饼的总数

小比萨饼的总数

订单的平均成本

当我编译我的程序时没有错误,当我运行它时,我得到了完美的提示……然而,在你输入答案后,程序只是重新开始,并从头开始提示。它不输出任何内容或进行任何计算。它甚至不会检查您输入的内容是否正确。我不确定哪里出了问题?这是我的逻辑,我的代码,还是两者兼而有之?有人能告诉我怎么解决这个问题吗。谢谢大家!

哦,如果这是有用的信息,我使用Notepad++在命令提示符下编译并运行它

    /*This program will keep prompting the user to enter their pizza order, perform 
  the requested calculation, and then output the requested result.
  Written by Hannah Lane*/

import java.util.Scanner;
public class PizzaOrders
{
  public static void main(String[] args)
  {
    Scanner input = new Scanner(System.in);
    int smallpizzas = 0, mediumpizzas = 0, largepizzas = 0, numberoforders = 0;
    double totalordercost = 0.0, pizzacost = 0.0, averagecost = 0.0;
    String custlastname = "", pizzasize = "", pizzatype ="", response = "";

    /*The loop will prompt the user to see if the customer would like to order a pizza. 
      If yes, it will prompt the user for their last name, choice of pizza type, and 
      their choice of pizza size. The two possible responses are yes and no. The dummy 
      value for the loop is no. */

      System.out.print("Do you want to order one pizza?" +
             "Type yes or no (all lower case), then press the ENTER key.");
             response = input.next();
             while (!(response.equals("no")))
             {
                /*Validate the user's response. If it is valid, prompt for the required 
                  values, perform the calculation, and output the result. If invalid, 
                  output an error message. */

                if (response.equals("yes"))
                {
                  System.out.println("Please type in your last name (it can only be one word) and then press the ENTER key.");
                  custlastname = input.next();
                  System.out.println("Please type in your choice of pizza in all lower case letters. What you type into the keyboard must be" +
                                     " pepperoni, veggie, cheese, or supreme.");
                  pizzatype = input.next();
                  System.out.println("Please type in your choice of pizza size in all lower case letters. What you type into the keyboard must" +
                                     " be small, medium, or large.");

                  pizzasize = input.next();

                  /* Validate the entries for the calculation. The sizes must be small, medium, or large. The types of
                     pizza must be pepperoni, veggie, cheese, or supreme. For division, we must make sure the denominator
                     is not zero. If invalid, output an error message. */

                  if (pizzatype.equals("pepperoni") || pizzatype.equals("veggie") || pizzatype.equals("cheese") ||
                      pizzatype.equals("supreme") && pizzasize.equals("small") || pizzasize.equals("medium") ||
                      pizzasize.equals("large") && numberoforders !=0.0) 
                    {
                       if (pizzatype.equals ("pepperoni"))
                       {
                           if (pizzasize.equals ("small"))
                           {
                             smallpizzas = smallpizzas + 1;
                             pizzacost = 8.50;
                             totalordercost = totalordercost + 8.50;
                             numberoforders = numberoforders + 1;
                            }
                           else if (pizzasize.equals ("medium"))
                           {
                             mediumpizzas = mediumpizzas + 1;
                             pizzacost = 9.50;
                             totalordercost = totalordercost + 9.50;
                             numberoforders = numberoforders + 1;
                            }
                            else if(pizzasize.equals ("large"))
                            {
                             largepizzas = largepizzas + 1;
                             pizzacost = 10.50;
                             totalordercost = totalordercost + 10.50;
                             numberoforders = numberoforders + 1;
                            }
                        }
                        else if (pizzatype.equals ("veggie"))
                        {
                          if (pizzasize.equals ("small"))
                          {
                            smallpizzas = smallpizzas + 1;
                            pizzacost = 10.00;
                            totalordercost = totalordercost + 10.00;
                            numberoforders = numberoforders + 1;
                           }
                           else if (pizzasize.equals ("medium"))
                           {
                             mediumpizzas = mediumpizzas + 1;
                             pizzacost = 12.25;
                             totalordercost = totalordercost + 12.25;
                             numberoforders = numberoforders + 1;
                            }
                            else if (pizzasize.equals ("large"))
                            {
                             largepizzas = largepizzas + 1;
                             pizzacost = 14.50;
                             totalordercost = totalordercost + 14.50;
                             numberoforders = numberoforders + 1;   
                            }
                        }
                        else if (pizzatype.equals ("cheese"))
                        {
                          if (pizzasize.equals ("small"))
                          {
                            smallpizzas = smallpizzas + 1;
                            pizzacost = 7.00;
                            totalordercost = totalordercost + 7.00;
                            numberoforders = numberoforders + 1;
                           }
                            else if (pizzasize.equals ("medium"))
                           {
                             mediumpizzas = mediumpizzas + 1;
                             pizzacost = 8.00;
                             totalordercost = totalordercost + 8.00;
                             numberoforders = numberoforders + 1;
                            }
                            else if (pizzasize.equals ("large"))
                            {
                             largepizzas = largepizzas + 1;
                             pizzacost = 9.00;
                             totalordercost = totalordercost + 9.00;
                             numberoforders = numberoforders + 1;
                            }
                        }
                        else if (pizzatype.equals ("supreme"))
                        {
                          if (pizzasize.equals ("small"))
                          {
                            smallpizzas = smallpizzas + 1;
                            pizzacost = 11.00;
                            totalordercost = totalordercost + 11.00;
                            numberoforders = numberoforders + 1;
                           }
                           else if (pizzasize.equals ("medium"))
                           {
                             mediumpizzas = mediumpizzas + 1;
                             pizzacost = 14.00;
                             totalordercost = totalordercost + 14.00;
                             numberoforders = numberoforders + 1;
                            }
                            else if (pizzasize.equals ("large"))
                            {
                             largepizzas = largepizzas + 1;
                             pizzacost = 16.00;
                             totalordercost = totalordercost + 16.00;
                             numberoforders = numberoforders + 1;
                            }
                          averagecost = totalordercost/(double)numberoforders;
                          System.out.println(custlastname + pizzacost + smallpizzas + mediumpizzas + largepizzas + averagecost);
                        }
                    }
                }
                  else
                      System.out.println("What you have typed in is incorrect. Your response must be yes or no.");

                  System.out.println("Do you want to order one pizza? Type yes or no" +
                          "(all lower case), then press the ENTER key.");
                  response = input.next();
            }

   }
}

只有在用户购买supreme small pizza时,才会执行打印订单的代码。检查打印行的位置,它位于if-else结构中。你应该把它移低一点(就在最后一个之前)。检查下面的代码,我移动了以下行:

System.out.println(custlastname + pizzacost + smallpizzas + mediumpizzas + largepizzas + averagecost);
完整代码:

import java.util.Scanner;
public class PizzaOrders
{
  public static void main(String[] args)
  {
    Scanner input = new Scanner(System.in);
    int smallpizzas = 0, mediumpizzas = 0, largepizzas = 0, numberoforders = 0;
    double totalordercost = 0.0, pizzacost = 0.0, averagecost = 0.0;
    String custlastname = "", pizzasize = "", pizzatype ="", response = "";

    /*The loop will prompt the user to see if the customer would like to order a pizza. 
      If yes, it will prompt the user for their last name, choice of pizza type, and 
      their choice of pizza size. The two possible responses are yes and no. The dummy 
      value for the loop is no. */

      System.out.print("Do you want to order one pizza?" +
             "Type yes or no (all lower case), then press the ENTER key.");
             response = input.next();
             while (!(response.equals("no")))
             {
                /*Validate the user's response. If it is valid, prompt for the required 
                  values, perform the calculation, and output the result. If invalid, 
                  output an error message. */

                if (response.equals("yes"))
                {
                  System.out.println("Please type in your last name (it can only be one word) and then press the ENTER key.");
                  custlastname = input.next();
                  System.out.println("Please type in your choice of pizza in all lower case letters. What you type into the keyboard must be" +
                                     " pepperoni, veggie, cheese, or supreme.");
                  pizzatype = input.next();
                  System.out.println("Please type in your choice of pizza size in all lower case letters. What you type into the keyboard must" +
                                     " be small, medium, or large.");

                  pizzasize = input.next();

                  /* Validate the entries for the calculation. The sizes must be small, medium, or large. The types of
                     pizza must be pepperoni, veggie, cheese, or supreme. For division, we must make sure the denominator
                     is not zero. If invalid, output an error message. */

                  if (pizzatype.equals("pepperoni") || pizzatype.equals("veggie") || pizzatype.equals("cheese") ||
                      pizzatype.equals("supreme") && pizzasize.equals("small") || pizzasize.equals("medium") ||
                      pizzasize.equals("large") && numberoforders !=0.0) 
                    {
                       if (pizzatype.equals ("pepperoni"))
                       {
                           if (pizzasize.equals ("small"))
                           {
                             smallpizzas = smallpizzas + 1;
                             pizzacost = 8.50;
                             totalordercost = totalordercost + 8.50;
                             numberoforders = numberoforders + 1;
                            }
                           else if (pizzasize.equals ("medium"))
                           {
                             mediumpizzas = mediumpizzas + 1;
                             pizzacost = 9.50;
                             totalordercost = totalordercost + 9.50;
                             numberoforders = numberoforders + 1;
                            }
                            else if(pizzasize.equals ("large"))
                            {
                             largepizzas = largepizzas + 1;
                             pizzacost = 10.50;
                             totalordercost = totalordercost + 10.50;
                             numberoforders = numberoforders + 1;
                            }
                        }
                        else if (pizzatype.equals ("veggie"))
                        {
                          if (pizzasize.equals ("small"))
                          {
                            smallpizzas = smallpizzas + 1;
                            pizzacost = 10.00;
                            totalordercost = totalordercost + 10.00;
                            numberoforders = numberoforders + 1;
                           }
                           else if (pizzasize.equals ("medium"))
                           {
                             mediumpizzas = mediumpizzas + 1;
                             pizzacost = 12.25;
                             totalordercost = totalordercost + 12.25;
                             numberoforders = numberoforders + 1;
                            }
                            else if (pizzasize.equals ("large"))
                            {
                             largepizzas = largepizzas + 1;
                             pizzacost = 14.50;
                             totalordercost = totalordercost + 14.50;
                             numberoforders = numberoforders + 1;   
                            }
                        }
                        else if (pizzatype.equals ("cheese"))
                        {
                          if (pizzasize.equals ("small"))
                          {
                            smallpizzas = smallpizzas + 1;
                            pizzacost = 7.00;
                            totalordercost = totalordercost + 7.00;
                            numberoforders = numberoforders + 1;
                           }
                            else if (pizzasize.equals ("medium"))
                           {
                             mediumpizzas = mediumpizzas + 1;
                             pizzacost = 8.00;
                             totalordercost = totalordercost + 8.00;
                             numberoforders = numberoforders + 1;
                            }
                            else if (pizzasize.equals ("large"))
                            {
                             largepizzas = largepizzas + 1;
                             pizzacost = 9.00;
                             totalordercost = totalordercost + 9.00;
                             numberoforders = numberoforders + 1;
                            }
                        }
                        else if (pizzatype.equals ("supreme"))
                        {
                          if (pizzasize.equals ("small"))
                          {
                            smallpizzas = smallpizzas + 1;
                            pizzacost = 11.00;
                            totalordercost = totalordercost + 11.00;
                            numberoforders = numberoforders + 1;
                           }
                           else if (pizzasize.equals ("medium"))
                           {
                             mediumpizzas = mediumpizzas + 1;
                             pizzacost = 14.00;
                             totalordercost = totalordercost + 14.00;
                             numberoforders = numberoforders + 1;
                            }
                            else if (pizzasize.equals ("large"))
                            {
                             largepizzas = largepizzas + 1;
                             pizzacost = 16.00;
                             totalordercost = totalordercost + 16.00;
                             numberoforders = numberoforders + 1;
                            }
                          averagecost = totalordercost/(double)numberoforders;
                        }
                    }
                    System.out.println(custlastname + pizzacost + smallpizzas + mediumpizzas + largepizzas + averagecost);
                }
                  else
                      System.out.println("What you have typed in is incorrect. Your response must be yes or no.");

                  System.out.println("Do you want to order one pizza? Type yes or no" +
                          "(all lower case), then press the ENTER key.");
                  response = input.next();
            }

   }
}

必须将System.out.println()放在else条件之前

averagecost = totalordercost/(double)numberoforders;
    //Remove this line====> System.out.println(custlastname + pizzacost + smallpizzas + mediumpizzas + largepizzas + averagecost); <=== remove this
                    }
                }
/*Add it here */ System.out.println(custlastname + pizzacost + smallpizzas + mediumpizzas + largepizzas + averagecost); //<== Add it here
            }
              else
                  System.out.println("What you have typed in is incorrect. Your response must be yes or no.");
averagecost=totalordercost/(双)订单数;

//删除此行==>System.out.println(custrastname+pizzacost+smallpizzas+mediumpizzas+largepizzas+averagecost) 对于输入验证,您需要以下内容:

boolean type = false;
 while (!type) {
                  System.out.println("Please type in your choice of pizza in all lower case letters. What you type into the keyboard must be" +
                          " pepperoni, veggie, cheese, or supreme.");
                  pizzatype = input.next();
                  if (pizzatype.equals("pepperoni") || pizzatype.equals("veggie") || pizzatype.equals("cheese") ||
                          pizzatype.equals("supreme")){
                      type = true;
                  }else{
                      System.out.println("What you have typed in is incorrect. Your response must be pepperoni, veggie, cheese, or supreme.");
                      type = false;
                  }
            }
type = false;
如果输入正确,您将继续,否则您将不断询问,直到输入正确


也许您可以将姓氏问题的位置更改为“while(!(response.equals(“no”)))”循环之前的位置。因此,如果我想要第二个比萨饼,我不必再次键入它。

这里是我的优化完整工作代码:

    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int smallpizzas = 0, mediumpizzas = 0, largepizzas = 0, numberoforders = 0;
    double totalordercost = 0.0, pizzacost = 0.0, averagecost = 0.0;
    String custlastname = "", pizzasize = "", pizzatype = "", response = "";
    boolean type = false;
    boolean size = false;
    /*
     * The loop will prompt the user to see if the customer would like to
     * order a pizza. If yes, it will prompt the user for their last name,
     * choice of pizza type, and their choice of pizza size. The two
     * possible responses are yes and no. The dummy value for the loop is
     * no.
     */

    System.out.print("Do you want to order one pizza?"
            + "Type yes or no (all lower case), then press the ENTER key.");
    response = input.next();
    while (!(response.equals("no"))) {
        /*
         * Validate the user's response. If it is valid, prompt for the
         * required values, perform the calculation, and output the result.
         * If invalid, output an error message.
         */

        if (response.equals("yes")) {
            if (custlastname.isEmpty()) {
                System.out
                        .println("Please type in your last name (it can only be one word) and then press the ENTER key.");
                custlastname = input.next();
            }
            while (!type) {
                System.out
                        .println("Please type in your choice of pizza in all lower case letters. What you type into the keyboard must be"
                                + " pepperoni, veggie, cheese, or supreme.");
                pizzatype = input.next();
                if (pizzatype.equals("pepperoni")
                        || pizzatype.equals("veggie")
                        || pizzatype.equals("cheese")
                        || pizzatype.equals("supreme")) {
                    type = true;
                } else {
                    System.out
                            .println("What you have typed in is incorrect. Your response must be pepperoni, veggie, cheese, or supreme.");
                    type = false;
                }
            }
            type = false;
            while (!size) {
                System.out
                        .println("Please type in your choice of pizza size in all lower case letters. What you type into the keyboard must"
                                + " be small, medium, or large.");

                pizzasize = input.next();
                if (pizzasize.equals("small") || pizzasize.equals("medium")
                        || pizzasize.equals("large")) {
                    size = true;
                } else {
                    System.out
                            .println("What you have typed in is incorrect. Your response must be small, medium, or large.");
                    size = false;
                }
            }
            size = false;

            /*
             * Validate the entries for the calculation. The sizes must be
             * small, medium, or large. The types of pizza must be
             * pepperoni, veggie, cheese, or supreme. For division, we must
             * make sure the denominator is not zero. If invalid, output an
             * error message.
             */

            if (pizzatype.equals("pepperoni")) {
                if (pizzasize.equals("small")) {
                    smallpizzas = smallpizzas + 1;
                    pizzacost = 8.50;
                    totalordercost = totalordercost + 8.50;
                } else if (pizzasize.equals("medium")) {
                    mediumpizzas = mediumpizzas + 1;
                    pizzacost = 9.50;
                    totalordercost = totalordercost + 9.50;
                } else if (pizzasize.equals("large")) {
                    largepizzas = largepizzas + 1;
                    pizzacost = 10.50;
                    totalordercost = totalordercost + 10.50;
                }
                numberoforders = numberoforders + 1;
            } else if (pizzatype.equals("veggie")) {
                if (pizzasize.equals("small")) {
                    smallpizzas = smallpizzas + 1;
                    pizzacost = 10.00;
                    totalordercost = totalordercost + 10.00;
                } else if (pizzasize.equals("medium")) {
                    mediumpizzas = mediumpizzas + 1;
                    pizzacost = 12.25;
                    totalordercost = totalordercost + 12.25;
                } else if (pizzasize.equals("large")) {
                    largepizzas = largepizzas + 1;
                    pizzacost = 14.50;
                    totalordercost = totalordercost + 14.50;
                }
                numberoforders = numberoforders + 1;
            } else if (pizzatype.equals("cheese")) {
                if (pizzasize.equals("small")) {
                    smallpizzas = smallpizzas + 1;
                    pizzacost = 7.00;
                    totalordercost = totalordercost + 7.00;
                } else if (pizzasize.equals("medium")) {
                    mediumpizzas = mediumpizzas + 1;
                    pizzacost = 8.00;
                    totalordercost = totalordercost + 8.00;
                } else if (pizzasize.equals("large")) {
                    largepizzas = largepizzas + 1;
                    pizzacost = 9.00;
                    totalordercost = totalordercost + 9.00;
                }
                numberoforders = numberoforders + 1;
            } else if (pizzatype.equals("supreme")) {
                if (pizzasize.equals("small")) {
                    smallpizzas = smallpizzas + 1;
                    pizzacost = 11.00;
                    totalordercost = totalordercost + 11.00;
                } else if (pizzasize.equals("medium")) {
                    mediumpizzas = mediumpizzas + 1;
                    pizzacost = 14.00;
                    totalordercost = totalordercost + 14.00;
                } else if (pizzasize.equals("large")) {
                    largepizzas = largepizzas + 1;
                    pizzacost = 16.00;
                    totalordercost = totalordercost + 16.00;
                }
                numberoforders = numberoforders + 1;
            }
            averagecost = totalordercost / (double) numberoforders;
            System.out.println(custlastname + " " + pizzacost + " "
                    + smallpizzas + " " + mediumpizzas + " " + largepizzas
                    + " " + averagecost + " " + totalordercost);
        } else
            System.out
                    .println("What you have typed in is incorrect. Your response must be yes or no.");

        System.out.println("Do you want to order one pizza? Type yes or no"
                + "(all lower case), then press the ENTER key.");
        response = input.next();
    }

}

我希望这能帮助你理解你的错误;)

谢谢你的回复!不幸的是,它除了提示之外什么也没做。似乎我的代码有问题,比如我没有正确地告诉它去计算。我只是不知道如何修复它,或者问题到底出在哪里。我添加了您的更正,但我的程序继续只提示,不做任何其他操作。我认为它没有正确读取计算命令。我只是不确定如何修复它,以及问题到底出在代码中。@Hannah我已经测试了你的代码,它可以很好地处理更改。当我用更改测试代码时,它让我输入所有信息,然后在输入我想要的比萨大小后(我选择了小比萨)点击回车键,它会返回第一个问题,它问我是否想要点比萨饼。在我的命令提示符下,它不显示任何类型的打印。你用什么来运行它?你会把它放在哪里?我试着用这个代替我的数据验证,但程序似乎没有改变。它仍然提示我没有输出。@Hannah:而不是
System.out.println(“请用所有小写字母键入您选择的比萨饼。您在键盘上键入的必须是“+”意大利香肠、蔬菜、奶酪或supreme。”);pizzatype=input.next()使用此循环。可能您在保存或重新编译更改时遇到问题?谢谢您的建议。我把它放在我的代码中,但在我的命令提示符下,当我运行它时,程序仍然会提示我,而不会输出任何东西。我想知道我代码中的计算是否没有被读取?我有你的错误:)你必须计算
averagecost=totalordercost/numberoforders
else if(pizzatype.equals(“supreme”)
的关闭}后向下一行。在此之后,它将正确计算。但是为什么不想查看总成本呢?出于某种原因,即使我将其向下移动一行,我的命令提示符仍然显示在运行它时没有进行任何计算…:/哈哈,没有总成本的原因只是因为作业不需要它。谢谢!我试试看。