即使没有错误,如何让我的java程序运行?我做错了什么?如何让程序运行?

时间:2015-10-15 04:17:57

标签: java windows-7-x64

在这个程序中,我应该询问用户是否要订购一个披萨,如果他们说是,他们必须输入:

their last name (don't worry about validating)

their choice of pizza type

their choice of pizza size.

选项为veggie, cheese, pepperoni, and supreme.您只能获得small, medium, or large.

每个人只能订一份披萨。您需要验证的只是初始响应,比萨饼类型和尺寸。当我编译我的程序时,我没有错误,但当我尝试运行它时,它所做的就是显示我的代码。当我说“它所做的一切都是显示我的代码”时,我的意思是在我使用的浏览器上显示了我在Notepad ++中看到的内容。我通过单击Notepad ++上的“运行”下拉按钮来运行它。它为我提供了在不同浏览器中运行它的选项。我尝试在mozilla和chrome中运行它,结果相同(仅显示源代码,而不是运行我的程序)。关于如何使我的程序工作的任何建议??

/*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();
            }

   }
}

1 个答案:

答案 0 :(得分:1)

你在运行之前编译过程序吗? 您必须在PC中安装JDK

编译和运行java程序的步骤 - 1)使用文本编辑器编写java代码并将文件保存为classname.java(在您的情况下,类名称为“PizzaOrders”)。

2)打开命令提示符并导航到保存上一个文件的文件夹。 (即PizzaOrders.java)。

3)运行命令javac PizzaOrders.java。

4)运行命令java PizzaOrders。