在java中导入用户定义的类

时间:2011-10-23 09:18:09

标签: java java-7

以下是测试代码,我收到错误Pizza order = new Pizza();我认为我没有将Pizza.class导入Pizzaorder.class文件。任何人都可以帮我解决这个错误。

代码如下。

Pizza.java

package pizza;
public class Pizza {
        private double cost; //the cost of the pizza
    private String crust; //the type of crust
    private int size; //the diameter in inches
    private int numToppings; //the number of toppings
    private String toppingList; //a list of the toppings
    public static void main(String[] args) {
    }
    public Pizza()
    {
        cost = 12.99;
        crust = "Hand-tossed";
        size = 12;
        numToppings = 0;
        toppingList = null;
    }
    public void setCost (double amount)
        {
            cost += amount;
        }
    public void setCrust (String type)
    {
        crust = type;
    }
    public void setSize (int diameter)
    {
        size = diameter;
    }
    public void setNumToppings(int number)
    {
        numToppings = number;
    }
    public void setToppingList (String newTopping)
    {
        toppingList = newTopping;
    }
    public double getCost()
    {
        return cost;
    }
    public String getCrust()
    {
        return crust;
    }
    public int getSize()
    {
        return size;
    }
    public int getNumToppings()
    {
        return numToppings;
    }
    public String getToppingList()
    {
        return toppingList;
    }
}

PizzaOrder.java

package pizza;
import java.util.Scanner;
public class PizzaOrder {
    public static void main (String [] args)
    {
                Scanner keyboard = new Scanner (System.in);
                Pizza order = new Pizza ();
                String firstName;
        boolean discount = false;
                int inches; 
        char crustType; 
        double cost; 
        final double TAX_RATE = .08;
        double tax;
        char choice; 
        String input;
        String toppings = "Cheese ";
        int numberOfToppings = 0;
        System.out.println("Welcome to Abdul and " +
        "Diane’s Pizza");
        System.out.print("Enter your first name: ");
        firstName = keyboard.nextLine();
        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.println("What size pizza would you like?");
        System.out.print("10, 12, 14, or 16 " + "(enter the number only): ");
        inches = keyboard.nextInt();
        keyboard.nextLine();
        System.out.println("What type of crust do you want? ");
        System.out.print(
        "(H)Hand-tossed, (T) Thin-crust, or " +
        "(D) Deep-dish (enter H, T, or D): ");
        input = keyboard.nextLine();
        crustType = input.charAt(0);
                System.out.println("All pizzas come with cheese.");
        System.out.println(
        "Additional toppings are £1.25 each,"
        + " choose from");
        System.out.println(
        "Pepperoni, Sausage, Onion, Mushroom");
        System.out.print("Do you want Pepperoni? (Y/N): ");
        input = keyboard.nextLine();
        choice = input.charAt(0);
        if (choice == 'Y' || choice == 'y')
        {
            numberOfToppings += 1;
            toppings = toppings + "Pepperoni ";
        }
        System.out.print("Do you want Sausage? (Y/N): ");
        input = keyboard.nextLine();

        choice = input.charAt(0);
        if (choice == 'Y' || choice == 'y')
        {
            numberOfToppings += 1;
            toppings = toppings + "Sausage ";
        }
        System.out.print("Do you want Onion? (Y/N): ");
        input = keyboard.nextLine();

        choice = input.charAt(0);
        if (choice == 'Y' || choice == 'y')
        {
            numberOfToppings += 1;
            toppings = toppings + "Onion ";
        }
        System.out.print("Do you want Mushroom? (Y/N): ");
        input = keyboard.nextLine();
        choice = input.charAt(0);
        if (choice == 'Y' || choice == 'y')
        {
            numberOfToppings += 1;
            toppings = toppings + "Mushroom ";
        }
        order.setNumToppings (numberOfToppings);
        order.setToppingList(toppings);
        order.setCost(1.25*numberOfToppings);
        System.out.println();
        System.out.println("Your order is as follows: ");
        System.out.println(order.getSize() + " inch pizza");
        System.out.println(order.getCrust() + " crust");
        System.out.println(order.getToppingList());
        cost = order.getCost();
                System.out.println("The cost of your order is: £" +
        cost);
        tax = cost * TAX_RATE;
        System.out.println("The tax is: £" + tax);
        System.out.println("The total due is: £" +
        (tax+cost));
        System.out.println("Your order will be ready" +
        " for pickup in 30 minutes.");
    }
}

错误:

C:\Users\Meutex\Documents\Netbeans projects>javac PizzaOrder.java
PizzaOrder.java:23: error: cannot find symbo

其他错误。

C:\Users\Meutex\Documents\Netbeans projects\Pizza>java Pizza.PizzaOrder
Exception in thread "main" java.lang.NoClassDefFoundError: Pizza/PizzaOrder (wro
ng name: pizza/PizzaOrder)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14
2)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:472)

5 个答案:

答案 0 :(得分:2)

问题是你没有编译这两个文件。

javac PizzaOrder.java更改为javac PizzaOrder.java Pizza.java

修改

要运行您的程序,请将PizzaOrder.classPizza.class放在名为pizza的目录中。从pizza运行java pizza.PizzaOrder的父目录。

答案 1 :(得分:1)

当您在包pizza中声明了类时,所有compiles类文件都应该放在文件夹pizza中。为此,请执行以下操作。

  1. 将文件保存在目录中,例如Project
  2. 在命令行上,转到Project文件夹。
  3. build文件夹中创建文件夹Project
  4. 将所有.java文件复制到Project文件夹。
  5. 在命令行中,使用-d选项编译代码。即执行javac -d build *.java 您将在pizza文件夹中看到另一个文件夹build
  6. 要运行该应用程序,请执行cd build
  7. 输入java pizza.PizzaOrder

答案 2 :(得分:0)

Pizza.java和PizzaOrder.java在包披萨中,因此应该在名为pizza的文件夹中。 如果您没有同时编译这两个文件,则必须设置类路径,以便编译器在编译PizzaOrder.java时可以找到pizza / Pizza.class。

更新:

假设当前目录是C:\ Users \ Meutex \ Documents \ Netbeans项目(顺便说一句,为什么你没有使用netbeans来编译你的项目),你的披萨目录在里面,然后命令是:< / p>

javac -cp . pizza/PizzaOrder.java

答案 3 :(得分:0)

以下是您的程序的输出:它似乎工作。 您是否将这两个课程都放在名为“披萨”的同一个文件夹中?

Welcome to Abdul and Diane’s Pizza
Enter your first name: K
Pizza Size (inches)     Cost
        10          £10.99
        12          £12.99
        14          £14.99
        16          £16.99
What size pizza would you like?
10, 12, 14, or 16 (enter the number only): 10
What type of crust do you want? 
(H)Hand-tossed, (T) Thin-crust, or (D) Deep-dish (enter H, T, or D): T
All pizzas come with cheese.
Additional toppings are £1.25 each, choose from
Pepperoni, Sausage, Onion, Mushroom
Do you want Pepperoni? (Y/N): Y
Do you want Sausage? (Y/N): Y
Do you want Onion? (Y/N): Y
Do you want Mushroom? (Y/N): Y

Your order is as follows: 
12 inch pizza
Hand-tossed crust
Cheese Pepperoni Sausage Onion Mushroom 
The cost of your order is: £17.990000000000002
The tax is: £1.4392000000000003
The total due is: £19.4292
Your order will be ready for pickup in 30 minutes.

关于程序的“风格”有很多考虑因素, 像大括号一样放置,在C方法的顶部定义变量,如C 而不是将主类划分为较小的方法,但对于初学者来说,没关系。

答案 4 :(得分:-1)

如果一个类有一个包,则不能简单地用扩展名为javac Pizza.java的程序名编译。必须创建一个目录,用于在其包中存储特定的类。它应该使用javac -d . Pizza.java命令进行编译,其中.将在当前目录中创建包的文件夹。要在其他位置创建文件夹,请提及位置名称,例如javac -d e:\user Pizza.java