gpt4 book ai didi

java - 创建带有月份的构造函数和方法

转载 作者:行者123 更新时间:2023-11-30 07:33:17 25 4
gpt4 key购买 nike

我修复了 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;
}

}

}

最佳答案

改变

inputMonthNumber.setMonthnum.input.nextInt(monthNumber);

inputMonthNumber.setMonthnum(monthNumber);

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

月份数 = currentMonthName;

Getter 旨在返回值。通过 getter 方法设置值并不是一个好的做法。 This文章解释了为什么需要 getter 和 setter 以及如何编写它们:)

关于java - 创建带有月份的构造函数和方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35759971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com