在java中导入用户定义的类

发布于 2024-12-11 07:52:35 字数 6382 浏览 2 评论 0原文

下面是一个测试代码,我收到错误 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)

The below is a test code where I am getting an error Pizza order = new Pizza(); I believe that I didn't import Pizza.class to Pizzaorder.class file. Can anybody help me in fixing this error.

Code follows.

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.");
    }
}

Error:

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

Additional error.

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

终陌 2024-12-18 07:52:35

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

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

编辑:

要运行您的程序,请输入 PizzaOrder.class< /code> 和 Pizza.class 位于名为 pizza 的目录中。从 pizza 的父目录运行 java Pizza.PizzaOrder

The problem is that you're not compiling both files.

Change javac PizzaOrder.java to be javac PizzaOrder.java Pizza.java

Edit:

To run your program, put PizzaOrder.class and Pizza.class in a directory called pizza. From the parent directory of pizza run java pizza.PizzaOrder.

左秋 2024-12-18 07:52:35

当您在 Pizza 包中声明了类后,所有编译类文件都应放入 Pizza 文件夹中。要实现此目的,请执行以下操作。

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

When you have declared the classes in the package pizza, all the compiles class files should go inside a folder pizza. To achieve this, do the following.

  1. Keep the files in a directory, say Project.
  2. On the command line, go to Project folder.
  3. Create a folder build inside the Project folder.
  4. Copy all your .java files to Project folder.
  5. On the command line, compile the code with -d option. i.e. execute javac -d build *.java
    You will see another folder pizza created inside the build folder.
  6. To run the application, do cd build.
  7. Type java pizza.PizzaOrder
与酒说心事 2024-12-18 07:52:35

以下是您的程序的输出: 它似乎有效。
您的两个课程是否都位于名为“pizza”的同一个文件夹中?

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 中一样,
并且没有将主类分成更小的方法,但是对于初学者来说,这是可以的。

Below is the output from your program: It seems to work.
Do you have both your classes in the same folder called "pizza"?

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.

There are a lot of considerations regarding the "style" of your program,
like curly brace-placement, defining variables at the top of the methods like in C,
and not dividing the main class into smaller methods, but for a beginner, it is ok.

我不在是我 2024-12-18 07:52:35

Pizza.java 和 PizzaOrder.java 位于 Pizza 包中,因此应位于名为 Pizza 的文件夹中。
如果您不一起编译这两个文件,则必须设置类路径,以便编译器在编译 PizzaOrder.java 时可以找到 Pizza/Pizza.class。

更新:

假设当前目录是 C:\Users\Meutex\Documents\Netbeans 项目(顺便说一句,为什么你不使用 netbeans 来编译你的项目),并且你的 Pizza 目录在其中,那么命令将是:

javac -cp . pizza/PizzaOrder.java

Pizza.java and PizzaOrder.java are in package pizza, therefore should be in a folder named pizza.
If you're not compiling both files together, the classpath must be set so that the compiler can find pizza/Pizza.class when compiling PizzaOrder.java.

UPDATE:

Let's say the current directory is C:\Users\Meutex\Documents\Netbeans projects (btw, why are You not using netbeans to compile your project), and your pizza directory is inside it, then the command would be:

javac -cp . pizza/PizzaOrder.java
面犯桃花 2024-12-18 07:52:35

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

If a class has a package it cannot be simply compiled by the program name with extension javac Pizza.java. A directory has to be created for storing the particular classes in its package. It should be compiled using javac -d . Pizza.java command where . will create the folder of package in the current directory. To create the folder in another location mention the location name like javac -d e:\user Pizza.java

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文