场景:我目前正在尝试用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;
}
}
答案 0 :(得分:0)
希望我能正确理解您的问题。
您的问题是您的用户可以在没有有效参数的情况下启动该方法。如果您有一个前端,那么也应该在那儿找到它。但是在后端(java)中,您可以使用try块或仅使用if-else。
将整数转换为字符串,然后使用正则表达式(也许像这样
String text = num + "";
if (text != null && text.matches("\\d+")) {
//...
}
或者您可以将Integer转换为String并使用String.isEmpty()。仅当length()为0时,isEmpty才返回true。(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
}
请注意,您的变量“月”应为“月”。在大多数编程社区中,每个人都使用小写变量。
有许多方法可以处理这种情况,您可以尝试其中的每一种。我希望我列出的都是正确的。
编辑: 哦,我检查了其他评论,发现您是初学者。这里有一些有用的提示,因此您班上的每个人,包括老师,都可以轻松阅读代码。在大多数编程语言中,都有类似“ 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;
}
}