다음은 오류가 발생하는 테스트 코드입니다 Pizza order = new Pizza();
Pizza.class를 Pizzaorder.class 파일로 가져 오지 않았다고 생각합니다. 아무도이 오류를 해결하는 데 나를 도울 수 있습니다.Java에서 사용자 정의 클래스 가져 오기
코드는 다음과 같습니다.
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)
과 같은 위치 이름을 언급하십시오. 왜 그런 문제가 있다고 생각합니까? 어떤 오류/문제가 있습니까? – Mat
나는 내 질문을 편집하여 철저히 조사하십시오. –
질문과 관련이 없지만 자바에서는 함수의 첫 번째 줄에 모든 변수를 선언 할 필요가 없습니다. 일반적으로 변수를 가능한 가장 가깝게 선언하는 것이 더 나은 방법입니다. [코드에서'main()'의'tax' 또는'cost' 변수에 대한 예 : – amit