【问题标题】:Where do I add a main method so that I can run this program properly?我在哪里添加一个 main 方法以便我可以正确运行这个程序?
【发布时间】:2021-01-18 14:14:29
【问题描述】:

我不知道在哪里添加 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 之于 Javascript 就像 Pain 之于绘画,或者 Ham 之于仓鼠。它们完全不同。强烈建议有抱负的程序员尝试学习他们尝试编写代码的语言的名称。当您发布问题时,请适当地标记它 - 这可以让那些了解您需要帮助的语言的人看到您的问题。
  • 很抱歉。 \-_-/

标签: java methods main-method


【解决方案1】:

在 Java 中,一切都是类。主要方法是这样写在类里面的:

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

【讨论】:

    【解决方案2】:

    将下面的 sn-p 添加到您的沙龙课程中。

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

    【讨论】:

      【解决方案3】:
      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();
           }
      }
      

      【讨论】:

      • 你可以复制这段代码并运行你的文件
      【解决方案4】:

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

      同时从沙龙类中删除公众 并将类 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(); 
      
      
           }
      
      
      }
      

      【讨论】:

      • class main() 似乎不是 java 的正确语法?无论如何,他的文件被命名为Salon.java,这样就不会编译顺便说一句
      • Salon 或任何类名之后加括号是java 中的一个新特性吗? afaik 那是语法错误吗?您还实例化了 Saloon 而不是 Salon
      • salon() 表示你已经创建了一个类对象
      • 为什么要创建一个单独的类来保存 main()?我只是把它作为主要课程的一部分。但你的拼写是对的。
      • 你可以在主类中编写干净的代码而不是太多东西
      猜你喜欢
      • 2023-03-16
      • 2019-09-07
      • 1970-01-01
      • 1970-01-01
      • 2011-08-26
      • 1970-01-01
      • 1970-01-01
      • 2020-06-21
      • 1970-01-01
      相关资源
      最近更新 更多