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

问题描述

我不知道在哪里添加 main 方法,以便我可以运行这个类。帮助!我也不知道把 main 方法放在哪里。

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

这是我的代码

 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: $0");
        System.out.println("Total Net Revenues: $" + dailyRev);
    }
}

 }

解决方法

在 Java 中,一切都是一个类。主要方法在类中编写如下:

[DllImport("user32",CharSet = CharSet.Auto,SetLastError = true)]
internal static extern bool GetMonitorInfo(
    IntPtr hmonitor,ref MonitorInfoEx info);

[StructLayout(LayoutKind.Sequential,Pack = 4)]
internal struct MonitorInfoEx
{
   public uint cbSize;
   public RECT rcMonitor;
   public RECT rcWork;
   public uint dwFlags;
   [MarshalAs(UnmanagedType.ByValArray,SizeConst = 32)]
   public char[] szDevice;
}

var info = new MonitorInfoEx
{
    cbSize = (uint)Marshal.SizeOf(typeof(MonitorInfoEx)),};
GetMonitorInfo(hDesktop,ref info);

在你的情况下:

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

将以下代码段添加到您的沙龙课程中。

public static void main(String[] args){
   Salon salon=new Salon();
   salon.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: $0");
            System.out.println("Total Net Revenues: $" + dailyRev);
        }
    }
}


public class Salon{

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

你可以在这个类下面再创建一个主类

同时从沙龙课程中删除 public 并将类 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(); 


     }


}