在哪里添加一个主要方法,以便我可以 运行 这个程序正常?

Where do I add a main method so that I can run this program properly?

我不知道在哪里添加主要方法所以我可以 运行 这个 class。帮助!我也不确定将主要方法放在哪里。

这位class经营一家美发沙龙。全局变量用于跟踪 dailyRevues(总收入)、dailyTax(总税额)以及理发、烫发和染发的次数。此外,要跟踪接受过服务的客户数量。要打印一份每日报告,其中提供客户总数、剪发、烫发和染发的总数。此外,还需要在日报中打印收入总额(收入)和税收总额。最后,应计算每个客户花费的平均金额并打印在报告中。如果商店带来 650 美元或更多的收入,那么商店经理将获得每日总收入的 5% 作为奖金。你应该在你的报告中包括商店经理的奖金,并从总收入中减去它,得到当天的净收入。随意添加您认为必要的其他变量和方法

Here is My Code

 import java.util.Scanner;

public class Salon {


See example report*/
//Globals
Scanner input = new Scanner(System.in);

int hairCuts = 0;
int permanents = 0;
int colorings = 0;
int customers = 0;

double dailyRev = 0;
double total = 0;
double dailyTax = 0;
double bonus; //money due the manager if certain conditions are met

final double COSTHC = 20;  //Cost of HairCut
final double COSTPERM = 50;  //Cost of Permanent
final double COSTCOLORING = 60;  //Cost of Coloring
final double COSTHC_PERM = (COSTHC * 0.85) + (COSTPERM * 0.85);  //Cost of Haircut and Permanent
final double COSTCOL_PERM = (COSTPERM * 0.85) + (COSTCOLORING * 0.85);  //Cost of Perm and Coloring
final double COSTHC_COL = (COSTHC * 0.85) + (COSTCOLORING * 0.85);  //Cost of Haircut and Coloring
final double COSTHC_PERM_COL = (COSTHC * 0.85) + (COSTPERM * 0.85) + (COSTCOLORING * 0.85);  //Cost of Cut, Perm and Color




public void menu() {
    //preconditions:  none
    //postconditions: the correct method is called depending on the user's
    //input, totalValues are updated appropriately
    int choice;
    do {
        System.out.println("---------MENU---------");
        System.out.println("1 =  Reset Daily Totals");
        System.out.println("2 =  Haircut Only");
        System.out.println("3 =  Permanent Only");
        System.out.println("4 =  Coloring Only");
        System.out.println("5 =  Haircut and Coloring");
        System.out.println("6 =  Haircut and Permanent");
        System.out.println("7 =  Permanent and Coloring");
        System.out.println("8 =  Haircut, Permanent and Coloring");
        System.out.println("9 =  Print Daily Report");
        System.out.println("10 = Exit");

        choice = input.nextInt();
        if (choice == 10) {
            System.exit(0);
        }

        if (choice == 1) {
            resetTotals();

        }
        if (choice == 2) {
            hairCuts++;
            customers++;
            dailyRev = dailyRev + COSTHC;
            dailyTax = 0.07 * dailyRev;
            total = dailyRev + dailyTax;
            System.out.println("Haircut only: $");
            System.out.println("Charge: $" + dailyRev);
            System.out.println("Tax: $" + dailyTax);
            System.out.println("Total: $" + total);

        }
        if (choice == 3) {
            permanents++;
            customers++;
            dailyRev = dailyRev + COSTPERM;
            dailyTax = 0.07 * dailyRev;
            total = dailyRev + dailyTax;
            System.out.println("Permanent only: $");
            System.out.println("Charge: $" + dailyRev);
            System.out.println("Tax: $" + dailyTax);
            System.out.println("Total: $" + total);

        }
        if (choice == 4) {
            colorings++;
            customers++;
            dailyRev = dailyRev + COSTCOLORING;
            dailyTax = 0.07 * dailyRev;
            total = dailyRev + dailyTax;
            System.out.println("Coloring only: $");
            System.out.println("Charge: $" + dailyRev);
            System.out.println("Tax: $" + dailyTax);
            System.out.println("Total: $" + total);

        }
        if (choice == 5) {
            hairCuts++;
            colorings++;
            customers++;
            dailyRev = dailyRev + COSTHC_COL;
            dailyTax = 0.07 * dailyRev;
            total = dailyRev + dailyTax;
            System.out.println("Haircut and Coloring: $");
            System.out.println("Charge: $" + dailyRev);
            System.out.println("Tax: $" + dailyTax);
            System.out.println("Total: $" + total);

        }
        if (choice == 6) {
            hairCuts++;
            permanents++;
            customers++;
            dailyRev = dailyRev + COSTHC_PERM;
            dailyTax = 0.07 * dailyRev;
            total = dailyRev + dailyTax;
            System.out.println("Haircut and Permanent: $");
            System.out.println("Charge: $" + dailyRev);
            System.out.println("Tax: $" + dailyTax);
            System.out.println("Total: $" + total);

        }
        if (choice == 7) {
            permanents++;
            colorings++;
            customers++;
            dailyRev = dailyRev + COSTCOL_PERM;
            dailyTax = 0.07 * dailyRev;
            total = dailyRev + dailyTax;
            System.out.println("Coloring and Permanent: $");
            System.out.println("Charge: $" + dailyRev);
            System.out.println("Tax: $" + dailyTax);
            System.out.println("Total: $" + total);

        }
        if (choice == 8) {
            hairCuts++;
            colorings++;
            permanents++;
            customers++;
            dailyRev = dailyRev + COSTHC_PERM_COL;
            dailyTax = 0.07 * dailyRev;
            total = dailyRev + dailyTax;
            System.out.println("haircut, Coloring, and Permanent: $");
            System.out.println("Charge: $" + dailyRev);
            System.out.println("Tax: $" + dailyTax);
            System.out.println("Total: $" + total);

        }
        if (choice == 9) {
            printReport();
        }
    } while (true);
}

public void resetTotals() {
    hairCuts = 0;
    permanents = 0;
    colorings = 0;
    customers = 0;
    dailyRev = 0;
    dailyTax = 0;
}

public void printReport() {
    if (customers == 0) {
        return;
    }
    System.out.println("Daily Report for Salon");
    System.out.println("Number of Customers: $" + customers);
    System.out.println("Number of Hair Cuts: $" + hairCuts);
    System.out.println("Number of Permanents: $" + permanents);
    System.out.println("Number of Colorings: $" + colorings);

    System.out.println("Average amount spent per customer(Not including Tax): $" + (total / customers));
    System.out.println("Total Revenues: $" + dailyRev);
    System.out.println("Total Tax: $" + dailyTax);
    if (dailyRev >= 650) {
        System.out.println("Manager Bonus: $" + (dailyRev * 0.05));
        System.out.println("Total Net Revenues: $" + 1.05 * dailyRev);
    } else {
        System.out.println("Manager Bonus: [=11=]");
        System.out.println("Total Net Revenues: $" + dailyRev);
    }
}

 }

在Java里,一切都是class。主要方法写在class里面:

public class Main
{
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

你的情况是:

import java.util.Scanner;

public class Salon {
    /* See example report */
    //Globals
    Scanner input = new Scanner(System.in);
    
    int hairCuts = 0;
    int permanents = 0;
    int colorings = 0;
    int customers = 0;
    
    double dailyRev = 0;
    double total = 0;
    double dailyTax = 0;
    double bonus; //money due the manager if certain conditions are met
    
    final double COSTHC = 20;  //Cost of HairCut
    final double COSTPERM = 50;  //Cost of Permanent
    final double COSTCOLORING = 60;  //Cost of Coloring
    final double COSTHC_PERM = (COSTHC * 0.85) + (COSTPERM * 0.85);  //Cost of Haircut and Permanent
    final double COSTCOL_PERM = (COSTPERM * 0.85) + (COSTCOLORING * 0.85);  //Cost of Perm and Coloring
    final double COSTHC_COL = (COSTHC * 0.85) + (COSTCOLORING * 0.85);  //Cost of Haircut and Coloring
    final double COSTHC_PERM_COL = (COSTHC * 0.85) + (COSTPERM * 0.85) + (COSTCOLORING * 0.85);  //Cost of Cut, Perm and Color
    
    
    public static void main(String[] args) {
        Salon s = new Salon();
        s.menu();
        // add your code here
    }
    
    public void menu() {
        //preconditions:  none
        //postconditions: the correct method is called depending on the user's
        //input, totalValues are updated appropriately
        int choice;
        do {
            System.out.println("---------MENU---------");
            System.out.println("1 =  Reset Daily Totals");
            System.out.println("2 =  Haircut Only");
            System.out.println("3 =  Permanent Only");
            System.out.println("4 =  Coloring Only");
            System.out.println("5 =  Haircut and Coloring");
            System.out.println("6 =  Haircut and Permanent");
            System.out.println("7 =  Permanent and Coloring");
            System.out.println("8 =  Haircut, Permanent and Coloring");
            System.out.println("9 =  Print Daily Report");
            System.out.println("10 = Exit");
    
            choice = input.nextInt();
            if (choice == 10) {
                System.exit(0);
            }
    
            if (choice == 1) {
                resetTotals();
    
            }
            if (choice == 2) {
                hairCuts++;
                customers++;
                dailyRev = dailyRev + COSTHC;
                dailyTax = 0.07 * dailyRev;
                total = dailyRev + dailyTax;
                System.out.println("Haircut only: $");
                System.out.println("Charge: $" + dailyRev);
                System.out.println("Tax: $" + dailyTax);
                System.out.println("Total: $" + total);
    
            }
            if (choice == 3) {
                permanents++;
                customers++;
                dailyRev = dailyRev + COSTPERM;
                dailyTax = 0.07 * dailyRev;
                total = dailyRev + dailyTax;
                System.out.println("Permanent only: $");
                System.out.println("Charge: $" + dailyRev);
                System.out.println("Tax: $" + dailyTax);
                System.out.println("Total: $" + total);
    
            }
            if (choice == 4) {
                colorings++;
                customers++;
                dailyRev = dailyRev + COSTCOLORING;
                dailyTax = 0.07 * dailyRev;
                total = dailyRev + dailyTax;
                System.out.println("Coloring only: $");
                System.out.println("Charge: $" + dailyRev);
                System.out.println("Tax: $" + dailyTax);
                System.out.println("Total: $" + total);
    
            }
            if (choice == 5) {
                hairCuts++;
                colorings++;
                customers++;
                dailyRev = dailyRev + COSTHC_COL;
                dailyTax = 0.07 * dailyRev;
                total = dailyRev + dailyTax;
                System.out.println("Haircut and Coloring: $");
                System.out.println("Charge: $" + dailyRev);
                System.out.println("Tax: $" + dailyTax);
                System.out.println("Total: $" + total);
    
            }
            if (choice == 6) {
                hairCuts++;
                permanents++;
                customers++;
                dailyRev = dailyRev + COSTHC_PERM;
                dailyTax = 0.07 * dailyRev;
                total = dailyRev + dailyTax;
                System.out.println("Haircut and Permanent: $");
                System.out.println("Charge: $" + dailyRev);
                System.out.println("Tax: $" + dailyTax);
                System.out.println("Total: $" + total);
    
            }
            if (choice == 7) {
                permanents++;
                colorings++;
                customers++;
                dailyRev = dailyRev + COSTCOL_PERM;
                dailyTax = 0.07 * dailyRev;
                total = dailyRev + dailyTax;
                System.out.println("Coloring and Permanent: $");
                System.out.println("Charge: $" + dailyRev);
                System.out.println("Tax: $" + dailyTax);
                System.out.println("Total: $" + total);
    
            }
            if (choice == 8) {
                hairCuts++;
                colorings++;
                permanents++;
                customers++;
                dailyRev = dailyRev + COSTHC_PERM_COL;
                dailyTax = 0.07 * dailyRev;
                total = dailyRev + dailyTax;
                System.out.println("haircut, Coloring, and Permanent: $");
                System.out.println("Charge: $" + dailyRev);
                System.out.println("Tax: $" + dailyTax);
                System.out.println("Total: $" + total);
    
            }
            if (choice == 9) {
                printReport();
            }
        } while (true);
    }
    
    public void resetTotals() {
        hairCuts = 0;
        permanents = 0;
        colorings = 0;
        customers = 0;
        dailyRev = 0;
        dailyTax = 0;
    }
    
    public void printReport() {
        if (customers == 0) {
            return;
        }
        System.out.println("Daily Report for Salon");
        System.out.println("Number of Customers: $" + customers);
        System.out.println("Number of Hair Cuts: $" + hairCuts);
        System.out.println("Number of Permanents: $" + permanents);
        System.out.println("Number of Colorings: $" + colorings);
    
        System.out.println("Average amount spent per customer(Not including Tax): $" + (total / customers));
        System.out.println("Total Revenues: $" + dailyRev);
        System.out.println("Total Tax: $" + dailyTax);
        if (dailyRev >= 650) {
            System.out.println("Manager Bonus: $" + (dailyRev * 0.05));
            System.out.println("Total Net Revenues: $" + 1.05 * dailyRev);
        } else {
            System.out.println("Manager Bonus: [=11=]");
            System.out.println("Total Net Revenues: $" + dailyRev);
        }
    }
}

你可以在这个 class

下面创建另一个主 class

同时从沙龙 Class 中删除 public 并将 class Salon 重命名为 Saloon

public class Saloon()
{
     public static void main(String args[]){

      //Now create an object of class above 
      Saloon s1 = new Saloon();

     // Now call the method you want
     s1.menu(); 


     }


}
import java.util.Scanner;

class Saloon {
    /* See example report */
    //Globals
    Scanner input = new Scanner(System.in);
    
    int hairCuts = 0;
    int permanents = 0;
    int colorings = 0;
    int customers = 0;
    
    double dailyRev = 0;
    double total = 0;
    double dailyTax = 0;
    double bonus; //money due the manager if certain conditions are met
    
    final double COSTHC = 20;  //Cost of HairCut
    final double COSTPERM = 50;  //Cost of Permanent
    final double COSTCOLORING = 60;  //Cost of Coloring
    final double COSTHC_PERM = (COSTHC * 0.85) + (COSTPERM * 0.85);  //Cost of Haircut and Permanent
    final double COSTCOL_PERM = (COSTPERM * 0.85) + (COSTCOLORING * 0.85);  //Cost of Perm and Coloring
    final double COSTHC_COL = (COSTHC * 0.85) + (COSTCOLORING * 0.85);  //Cost of Haircut and Coloring
    final double COSTHC_PERM_COL = (COSTHC * 0.85) + (COSTPERM * 0.85) + (COSTCOLORING * 0.85);  //Cost of Cut, Perm and Color
    
    public void menu() {
        //preconditions:  none
        //postconditions: the correct method is called depending on the user's
        //input, totalValues are updated appropriately
        
        
        do {
            System.out.println("---------MENU---------");
            System.out.println("1 =  Reset Daily Totals");
            System.out.println("2 =  Haircut Only");
            System.out.println("3 =  Permanent Only");
            System.out.println("4 =  Coloring Only");
            System.out.println("5 =  Haircut and Coloring");
            System.out.println("6 =  Haircut and Permanent");
            System.out.println("7 =  Permanent and Coloring");
            System.out.println("8 =  Haircut, Permanent and Coloring");
            System.out.println("9 =  Print Daily Report");
            System.out.println("10 = Exit");
            
            
            int choice;
            choice = input.nextInt();
            
            // if (choice == 10) {
            //     System.exit(0);
            // }
    
            if (choice == 1) {
                resetTotals();
    
            }
            if (choice == 2) {
                hairCuts++;
                customers++;
                dailyRev = dailyRev + COSTHC;
                dailyTax = 0.07 * dailyRev;
                total = dailyRev + dailyTax;
                System.out.println("Haircut only: $");
                System.out.println("Charge: $" + dailyRev);
                System.out.println("Tax: $" + dailyTax);
                System.out.println("Total: $" + total);
    
            }
            if (choice == 3) {
                permanents++;
                customers++;
                dailyRev = dailyRev + COSTPERM;
                dailyTax = 0.07 * dailyRev;
                total = dailyRev + dailyTax;
                System.out.println("Permanent only: $");
                System.out.println("Charge: $" + dailyRev);
                System.out.println("Tax: $" + dailyTax);
                System.out.println("Total: $" + total);
    
            }
            if (choice == 4) {
                colorings++;
                customers++;
                dailyRev = dailyRev + COSTCOLORING;
                dailyTax = 0.07 * dailyRev;
                total = dailyRev + dailyTax;
                System.out.println("Coloring only: $");
                System.out.println("Charge: $" + dailyRev);
                System.out.println("Tax: $" + dailyTax);
                System.out.println("Total: $" + total);
    
            }
            if (choice == 5) {
                hairCuts++;
                colorings++;
                customers++;
                dailyRev = dailyRev + COSTHC_COL;
                dailyTax = 0.07 * dailyRev;
                total = dailyRev + dailyTax;
                System.out.println("Haircut and Coloring: $");
                System.out.println("Charge: $" + dailyRev);
                System.out.println("Tax: $" + dailyTax);
                System.out.println("Total: $" + total);
    
            }
            if (choice == 6) {
                hairCuts++;
                permanents++;
                customers++;
                dailyRev = dailyRev + COSTHC_PERM;
                dailyTax = 0.07 * dailyRev;
                total = dailyRev + dailyTax;
                System.out.println("Haircut and Permanent: $");
                System.out.println("Charge: $" + dailyRev);
                System.out.println("Tax: $" + dailyTax);
                System.out.println("Total: $" + total);
    
            }
            if (choice == 7) {
                permanents++;
                colorings++;
                customers++;
                dailyRev = dailyRev + COSTCOL_PERM;
                dailyTax = 0.07 * dailyRev;
                total = dailyRev + dailyTax;
                System.out.println("Coloring and Permanent: $");
                System.out.println("Charge: $" + dailyRev);
                System.out.println("Tax: $" + dailyTax);
                System.out.println("Total: $" + total);
    
            }
            if (choice == 8) {
                hairCuts++;
                colorings++;
                permanents++;
                customers++;
                dailyRev = dailyRev + COSTHC_PERM_COL;
                dailyTax = 0.07 * dailyRev;
                total = dailyRev + dailyTax;
                System.out.println("haircut, Coloring, and Permanent: $");
                System.out.println("Charge: $" + dailyRev);
                System.out.println("Tax: $" + dailyTax);
                System.out.println("Total: $" + total);
    
            }
            if (choice == 9) {
                printReport();
            }
        } while (true);
    }
    
    public void resetTotals() {
        hairCuts = 0;
        permanents = 0;
        colorings = 0;
        customers = 0;
        dailyRev = 0;
        dailyTax = 0;
    }
    
    public void printReport() {
        if (customers == 0) {
            return;
        }
        System.out.println("Daily Report for Salon");
        System.out.println("Number of Customers: $" + customers);
        System.out.println("Number of Hair Cuts: $" + hairCuts);
        System.out.println("Number of Permanents: $" + permanents);
        System.out.println("Number of Colorings: $" + colorings);
    
        System.out.println("Average amount spent per customer(Not including Tax): $" + (total / customers));
        System.out.println("Total Revenues: $" + dailyRev);
        System.out.println("Total Tax: $" + dailyTax);
        if (dailyRev >= 650) {
            System.out.println("Manager Bonus: $" + (dailyRev * 0.05));
            System.out.println("Total Net Revenues: $" + 1.05 * dailyRev);
        } else {
            System.out.println("Manager Bonus: [=10=]");
            System.out.println("Total Net Revenues: $" + dailyRev);
        }
    }
}


public class Salon{

     public static void main(String []args){
         
       Saloon s = new Saloon();
      s.menu();
     }
}

将以下代码段添加到您的沙龙 class。

public static void main(String[] args){
   Salon salon=new Salon();
   salon.menu();
}