检查是否为方法定义了整数参数

Checking if an integer argument is defined for a method

场景:我目前正在尝试在Java中制作日历,这是我创建的方法之一(getMonthSize) 就是得到一个月的大小(如图)。我目前的目标是无论参数是否由用户定义都使该方法响应(如果参数留空则给出当前月份的大小,如果定义了参数则给出输入月份的大小)。

努力:我已经在网络上进行了相当彻底的搜索,我遇到了答案测试以查看 arg.length==0 (尽管事实证明只有当 arg 是一个字符串时才有效),并且使用类似于“arg!==undefined”(我认为可能适用于 javascript)的内容来回答,但无济于事。我还了解到,如果一个整数没有被初始化,它的默认值将为 0,所以这就是我试图利用我的代码显示的内容,尽管 java 似乎仍然希望我输入“0 " 让方法起作用。

问题: 我希望有一些命令可以检查参数是否留空,允许我设置 2 种情况,并通过该方法实现我想要的。

package model;

import java.util.Calendar;

public class myCalendar {
   private Calendar calendar=Calendar.getInstance();
   private int day=calendar.get(Calendar.DAY_OF_WEEK);
   private int date=this.calendar.get(Calendar.DAY_OF_MONTH);
   private int month=calendar.get(Calendar.MONTH)+1;
   private int year=calendar.get(Calendar.YEAR);
   
   public void setCalendar(int Year, int Month, int Date){
       this.calendar.set(Year,Month-1,Date);
   }
   
   public int getDay(){
      return day;
   }
   
   public int getDate () {
       return date;
   }
   
   public int getMonth(){
       return month;
   }
   
   public int getYear() {
       return year;
   }
   
   public int getFirstDay () {
       Calendar test = this.calendar;
       test.set(year, month-1, 1);
       return test.get(Calendar.DAY_OF_WEEK)-1;
   }
   
   public int getMonthSize (int num) {
       int Month=0;
       int days=0;
       if (num>0){
       Month=num;
       } else {
       Month=month;
       }
       if(Month==1||Month==3||Month==5||Month==7  
       ||Month==8||Month==10||Month==12){   
        days=31;   
      }   
        if(Month==4||Month==6||Month==9||Month==11)   
      {   
        days=30;   
      }   
      if(Month==2)   
      {   
        if(((year%4==0)&&(year%100!=0))||(year%400==0))   
        {   
          days=29;   
        }   
        else   
        {   
          days=28;   
        }   
      }
      return days;
   }
   
   public String getNow () {
       String monthNames[]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
           String a=monthNames[month-1]+", "+String.valueOf(year);
       return a;
   }
   
}

希望我正确理解了您的问题。

您的问题是您的用户可以在没有有效参数的情况下启动该方法。如果你有一个前端,那么你也应该在那里找到它。但在后端 (java),您可以使用 try-block 或简单地使用 if-else.

将整数转换为字符串,然后使用正则表达式(可能是这样的

String text = num + "";
if (text != null && text.matches("\d+")) {
    //...
}

或者您可以将整数转换为字符串并使用 String.isEmpty().isEmpty returns 当且仅当 length() 为 0 时为真。(https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#isEmpty()) .

Sting temp = num + "";
if (num.isEmpty()) {
    //...
}

或者只是一个 try-catch 块,您可以在其中将输入转换为新变量。

try {
    Month = Integer.parseInt(num);
} catch (NumberFormatException e) {
    // handle error
}

请注意您的变量“Month”应该是“month”。在大多数编程社区中,每个人都使用小写变量。

处理这种情况的方法有无数种,您可以尝试每一种。希望我列出的都是正确的。

编辑: 哦,我查看了其他评论,发现您是初学者。这里可能有一些有用的提示,因此 class 中的每个人(包括老师)都可以更轻松地阅读您的代码。在大多数编程语言中,都有类似“checkstyle”的东西。很难解释,但通过一些 google 您可以找到有用的提示。

我会对你的代码提出一些“建议”,你可以采纳也可以忽略。比如一些字符之间的空格,一些评论等。 但不要过分关注质量。在某些 IDE 中,您可以自动将代码格式化为“标准”。有了 Intellij 就可以了。 ctrl+alt+l

import java.util.Calendar;

public class myCalendar {
    private Calendar calendar = Calendar.getInstance();
    private int day = calendar.get(Calendar.DAY_OF_WEEK);
    private int date = this.calendar.get(Calendar.DAY_OF_MONTH);
    private int month = calendar.get(Calendar.MONTH) + 1;
    private int year = calendar.get(Calendar.YEAR);

    /**
     * Description what the method does.
     *
     * @param Year  what does the parameter?
     * @param Month ""
     * @param Date  ""
     */
    public void setCalendar(int Year, int Month, int Date) {
        this.calendar.set(Year, Month - 1, Date);
    }

    public int getDay() {
        return day;
    }

    public int getDate() {
        return date;
    }

    public int getMonth() {
        return month;
    }

    public int getYear() {
        return year;
    }

    /**
     * Description what the method does.
     *
     * @return value (short description)
     */
    public int getFirstDay() {
        Calendar test = this.calendar;
        test.set(year, month - 1, 1);
        return test.get(Calendar.DAY_OF_WEEK) - 1;
    }

    /**
     * Description what the method does.
     *
     * @param num (short description)
     * @return (short description)
     */
    public int getMonthSize(int num) {
        int month;
        int days = 0; //My IDE tells me that need to be initialized, but why? Are there cases where the method returns 0?
        //I think that num=100 returns 0, so maybe you can catch this case with if(num > 12) or something similar
        if (num > 0) {
            month = num;
        } else {
            month = this.month;
        }
        if (month == 1 || month == 3 || month == 5 || month == 7
            || month == 8 || month == 10 || month == 12) {
            days = 31;
        }
        if (month == 4 || month == 6 || month == 9 || month == 11) {
            days = 30;
        }
        if (month == 2) {
            if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
                days = 29;
            } else {
                days = 28;
            }
        }
        return days;
    }

    public String getNow() {
        String monthNames[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
        String a = monthNames[month - 1] + ", " + String.valueOf(year);
        return a;
    }

}