提示找不到或加载主类时如何运行该程序?

时间:2019-05-10 06:09:02

标签: java class netbeans subclass

我对Java还是很陌生,我不断收到此错误,但无法弄清楚。

  

错误:找不到或加载主类   restaurantclient.RestaurantClient   Java结果:1

这是在Netbeans上,我确定我做对了所有事情。这是我为Java类编写的作业,非常感谢您的帮助。我已经在这个问题上工作了几天了。

import java.text.DecimalFormat;

public class RestaurantClient {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Restaurant r1;
        Restaurant r2;
        r1 = new Restaurant("PizzaHut", 152, (float) 4.95); //instantiating
        r2 = new Restaurant("Dominos", 10, (float) 3.657);
        System.out.print(r1.toString());
        System.out.print(r2.toString());
        r2.setPeopleServed(r1.getPeopleServed());
        r2.setAveragePrice(r1.getAverageMeal());
        if (r1.equals(r2)) //checking if equal
            System.out.println("\nThe two objects are the same");
        else
            System.out.println("\nThe two objects are NOT the same");
        r2.setName(r1.getName());
        if (r1.equals(r2)) //checking if equal
            System.out.println("\nThe two objects are the same");
        else
            System.out.println("\nThe two objects are NOT the same");
        DecimalFormat pricePattern = new DecimalFormat("$###,###,000.00");
        System.out.println("Tax paid per year: " + pricePattern.format(r1.averageTaxes()));

    }

}

//Restaurant.java

public class Restaurant extends Store {
    private int peopleServed;
    private float avgmeal;

    public Restaurant(String newName, int peopleServed, float avgmeal) {
        super(newName);
        setPeopleServed(peopleServed);
        setAveragePrice(avgmeal);
    }
    public void setPeopleServed(int newpeopleServed) {
        if (newpeopleServed >= 0)
            peopleServed = newpeopleServed; //mutator alows client to change the value of name
        else
            System.out.println("Number has to be greater than zero");
    }
    public void setAveragePrice(float newprice) {
        if (newprice >= 0)
            avgmeal = newprice; //mutator alows client to change the value of name
        else
            System.out.println("Number has to be greater than zero");
    }
    public int getPeopleServed() {
        return peopleServed; //accessor; returns current value
    }
    public float getAverageMeal() {
        return avgmeal; //accessor; returns current value
    }
    public String toString() {
        return "Name of store is:   " + super.getName() +
            "\nNumber of people served is   " + peopleServed +
            "\nAverage price per person is " + avgmeal + "\n";
    }

    public boolean equals(Object o) {
        if (!(o instanceof Restaurant))
            return false;
        else {
            Restaurant objRest = (Restaurant) o;
            if (avgmeal == objRest.avgmeal && peopleServed == objRest.peopleServed && super.equals(objRest))
                return true;
            else
                return false;

        }

    }
    public double averageTaxes() {
        double avgtaxes = (double)(super.taxrate * (peopleServed * avgmeal * 365));
        return avgtaxes; //accessor; returns current value
    }

}

//Store.java

public class Store {
    private String storename;
    public final float taxrate = (float) .08000;

    public Store(String newName) //constructor
    {
        setName(newName);
    }

    public String getName() {
        return storename; //accessor; returns current value
    }
    public void setName(String newName) {
        storename = newName; //mutator alows client to change the value of name
    }
    public String toString() {
        return "Name of store is:   " + storename;
    }
    public boolean equals(Object o) {
        if (!(o instanceof Store))
            return false;
        else {
            Store objStore = (Store) o;
            if (storename.equals(objStore.storename))
                return true;
            else
                return false;
        }
    }
}

2 个答案:

答案 0 :(得分:2)

系统正在寻找主类restaurantclient.RestaurantClient,因此在包RestaurantClient中有一个类restaurantClient,但是您的类RestaurantClient似乎在缺省包中。

答案 1 :(得分:1)

如果没有任何编译问题,请尝试此操作。

您可以:

  1. 右键单击项目节点,然后转到“设置配置”。
  2. 为您的应用程序选择主类。
  3. 然后清理并构建。

即使上述步骤不起作用,也请通过删除(索引)文件夹来删除Netbeans缓存

更多详细信息 遵循stackOverflow问题 Netbeans - Error: Could not find or load main class