用月创建构造函数和方法

时间:2016-03-02 23:03:41

标签: java eclipse oop

我修复了我的setMonthnum方法,但现在我的输入会根据我的输入将月号设置为0,13等。我需要知道如何去再次询问输入,而不是将我的Monthnum设置为错误的输入。如果您有任何建议可以简单地改进我的代码,请随时说出来!我的代码如下:

import java.util.Scanner;

public class whichMonth {

    public static void main(String args[]){

        Scanner input = new Scanner(System.in);
        System.out.println("Please enter the month name or number: ");
        int monthNumber = input.nextInt();


        //set up variable access to the class
        AnyMonth inputMonthNumber = new AnyMonth();

        // set the month number from user input
        inputMonthNumber.setMonthnum.input.nextInt(monthNumber);

        // get the month name from user input number
        String monthName = inputMonthNumber.getMonthName(monthNumber);
    }
}

class AnyMonth {

   int Monthnum;
   String monthName;

   public AnyMonth() {

      Monthnum = 1;
   }
   public AnyMonth(int currentMonthNumber) {

      Monthnum = currentMonthNumber;

      switch(currentMonthNumber) {
         case 1:
            Monthnum = 1;
            monthName = "January";
            System.out.println("January");
            break;
         case 2:
            Monthnum = 2;
            monthName = "February";
            System.out.println("February");
            break;
         case 3:
            Monthnum = 3;
            monthName = "March";
            System.out.println("March");
            break;
         case 4:
            Monthnum = 4;
            monthName = "April";
            System.out.println("April");
            break;
         case 5:
            Monthnum = 5;
            monthName = "May";
            System.out.println("May");
            break;
         case 6:
            Monthnum = 6;
            monthName = "June";
            System.out.println("June");
            break;         
         case 7:
            Monthnum = 7;
            monthName = "July";
            System.out.println("July");
            break;         
         case 8:
            Monthnum = 8;
            monthName = "August";
            System.out.println("August");
            break;         
         case 9:
            Monthnum = 9;
            monthName = "September";
            System.out.println("September");
            break;         
         case 10:
            Monthnum = 10;
            monthName = "October";
            System.out.println("October");
            break;
         case 11:
            Monthnum = 11;
            monthName = "November";
            System.out.println("November");
            break;
         case 12:
            Monthnum = 12;
            monthName = "December";
            System.out.println("December");
            break;
         default:
            Monthnum = 1;
            monthName = "January";
      } 

   } 

   public AnyMonth(String userMonthName) {

      switch(userMonthName) {
         case "January":
            Monthnum = 1;
            monthName = "January";
            System.out.println("January");
            break;
         case "February":
            Monthnum = 2;
            monthName = "February";
            System.out.println("February");
            break;
         case "March":
            Monthnum = 3;
            monthName = "March";
            System.out.println("March");
            break;
         case "April":
            Monthnum = 4;
            monthName = "April";
            System.out.println("April");
            break;
         case "May":
            Monthnum = 5;
            monthName = "May";
            System.out.println("May");
            break;
         case "June":
            Monthnum = 6;
            monthName = "June";
            System.out.println("June");
            break;         
         case "July":
            Monthnum = 7;
            monthName = "July";
            System.out.println("July");
            break;         
         case "August":
            Monthnum = 8;
            monthName = "August";
            System.out.println("August");
            break;         
         case "September":
            Monthnum = 9;
            monthName = "September";
            System.out.println("September");
            break;         
         case "October":
            Monthnum = 10;
            monthName = "October";
            System.out.println("October");
            break;
         case "November":
            Monthnum = 11;
            monthName = "November";
            System.out.println("November");
            break;
         case "December":
            Monthnum = 12;
            monthName = "December";
            System.out.println("December");
            break;
         default:
            Monthnum = 1;
            monthName = "January";

      }    

   }

   public void setMonthnum (int userMonth) {

      Monthnum = userMonth;

      if (userMonth < 1 || userMonth > 12){
         System.out.println("Invalid input");
         this.Monthnum = userMonth;
      } 

   }

   public int getMonthnum(int currentMonthNumber) {

      Monthnum = currentMonthNumber; 

      return currentMonthNumber;

   }

   public String getMonthName(int currentMonthName) {

      Monthnum = currentMonthName;
        // use swtich-case here instead, default case should return 1/Jan
      switch(currentMonthName) {
         case 1:
            monthName = "January";
            break;
         case 2:
            monthName = "February";;
            break;
         case 3:
            monthName = "March";
            break;
         case 4:
            monthName = "April";
            break;
         case 5:
            monthName = "May";
            break;
         case 6:
            monthName = "June";
            break;         
         case 7:
            monthName = "July";
            break;         
         case 8:
            monthName = "August";
            break;         
         case 9:
            monthName = "September";
            break;         
         case 10:
            monthName = "October";
            break;
         case 11:
            monthName = "November";
            break;
         case 12:
            monthName = "December";
            break;
         default:
            monthName = "January";

      }
      return monthName;
    }

    public String toString(String monthNameToString){

      monthName = monthNameToString;

      return monthNameToString;

    }

    public boolean equals(int month) {

        if (Monthnum == month) {
            return true;
        } 
        else {
            return false;
        }
    }

    public boolean greaterThan(int month) {

        if (Monthnum > month) {
            return true;
        } 
        else {
            return false;
        }
    }

    public boolean lessThan(int month) {

        if (Monthnum < month) {
            return true;
        } 
        else {
            return false;
        }

    }

}

1 个答案:

答案 0 :(得分:1)

更改

SELECT * FROM table1 WHERE integer NOT IN ( SELECT varchars FROM table2 WHERE id = 1 FIELDS ENCLOSED BY 'NULL')

inputMonthNumber.setMonthnum.input.nextInt(monthNumber);

此外,在所有getXXX方法中,删除以下语句:

inputMonthNumber.setMonthnum(monthNumber);

Getters被设计为返回值。通过getter方法设置值不是一个好习惯。 This文章解释了为什么需要吸气剂和制定者以及如何编写它们:)