2021年3月27日星期六

Main method issue java

I keep getting an error when it comes to my main method, it wants me to remove static from my main method then once I remove the static then I get an error saying I need public static void main(String [] args). Or adding static to the parent method which then ruins my cases. So I've hit a wall to be able to complete this program with no errors with the main method and using encapsulation. Getting error message The method main cannot be declared static; static methods can only be declared in a static or top level type

    public class Discussion2 {           public static void main(String [] args) {                 runStart.main(args);  }            private int income;          private int contributor;          private int monthly;                                      public Discussion2 (int income, int contributor, int monthly) {                            // Scanner              if (income <= 0) {                  System.out.println("Invalid income - Your income can't be 0 or less");              }                            if (contributor <= 0) {                  System.out.println("Invalid contributor - You must have at least 1 contributor");              }                            if (monthly <= 0) {                  System.out.println("Invalid monthly - Your monthly can't be 0 or less");              }                            this.income = income;              this.contributor = contributor;              this.monthly = monthly;              }                          public class runStart {                                    private runStart run;                                        public runStart() {                              run = new runStart();                          }                                                                             public void setIncome() {                             Scanner sc = new Scanner(System.in);                                                      System.out.println("What is your yearly income? ");                                                          income = sc.nextInt();                                                       }                                                  public void setContributor() {                             Scanner sc = new Scanner(System.in);                                                      System.out.println("How many contributors do you have? ");                                                          contributor = sc.nextInt();                                                      }                                                  public void determineMonthly() {                             Scanner sc = new Scanner(System.in);                                                      System.out.println("What is your monthly? ");                                                          monthly = (int) (income*.33/contributor);                                                          return;                                                      }                                                  public void displayMenu() {                                                            System.out.println("                    MENU");                              System.out.println("============================================");                              System.out.println("1: Set Income");                              System.out.println("2: Add Contributor");                              System.out.println("3: Determine Estimate");                              System.out.println("4: Exit Program");                            }                         public void choice(int Select) {                                                            switch (Select) {                                                            case 1 :    run.setIncome();                                          break;                                                                        case 2 :    run.setContributor();                                          break;                                                                        case 3 :    run.determineMonthly();                                          break;                                                                    case 4 :    System.out.println("Thank you, goodbye!");                                          break;                                                                                                      default :   System.err.println("Invalid Input");                                          break;                              }                         }                                       public static void main(String [] args) {                                                                                        runStart run = new runStart();                                                            try (Scanner sc = new Scanner(System.in)) {                                  int Select;                                                                            do {                                                                                    run.displayMenu();                                                                                    System.out.print("Input your selection from the menu: ");                                          Select = sc.nextInt();                                                                                    run.choice(Select);                                                                            } while (Select != 4);                                                                            sc.close();                              }                         }}                          
https://stackoverflow.com/questions/66837793/main-method-issue-java March 28, 2021 at 10:02AM

没有评论:

发表评论