我必须用Java创建一个月级 [英] I have to create a month class in Java
本文介绍了我必须用Java创建一个月级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
其中一个要求是 getMonthName
方法,该方法返回月份的名称,即1月为1.并且 toString
返回月份名称(String representsesentation),我只是担心我正在做的事情比这更简单:
One of the requirements is a getMonthName
method that returns the name if the month, i.e. January is 1. And a toString
that returns the month name (String represesentation) , and I'm just worried that there's an easier way for what I'm doing than this:
import java.util.Scanner;
import java.io.*;
public class Month {
private int monthNum = 0;
private String monthName;
String monthOne = "JANUARY";
String monthTwo = "FEBRUARY";
String monthThree = "MARCH";
String monthFour = "APRIL";
String monthFive = "MAY";
String monthSix = "JUNE";
String monthSeven = "JULY";
String monthEight = "AUGUST";
String monthNine = "SEPTEMBER";
String monthTen = "OCTOBER";
String monthEleven = "NOVEMBER";
String monthTwelve = "DECEMBER";
//CONSTRUCTORS
public Month()
{
monthNum = 1;
}
public Month(int monthNum)
{
this.monthNum = monthNum;
if ((monthNum > 12) || (monthNum <1))
{
monthNum = 1;
}
}
public Month(String monthOne, String monthTwo, String monthThree, String monthFour, String monthFive, String monthSix, String monthSeven,
String monthEight, String monthNine, String monthTen, String monthEleven, String monthTwelve)
{
monthName.toUpperCase();
if (monthName.equals(monthOne))
{
monthNum = 1;
}
else if (monthName.equals(monthTwo))
{
monthNum = 2;
}
else if (monthName.equals(monthThree))
{
monthNum = 3;
}
else if (monthName.equals(monthFour))
{
monthNum = 4;
}
else if (monthName.equals(monthFive))
{
monthNum = 5;
}
else if (monthName.equals(monthSix))
{
monthNum = 6;
}
else if (monthName.equals(monthSeven))
{
monthNum = 7;
}
else if (monthName.equals(monthEight))
{
monthNum = 8;
}
else if (monthName.equals(monthNine))
{
monthNum = 9;
}
else if (monthName.equals(monthTen))
{
monthNum = 10;
}
else if (monthName.equals(monthEleven))
{
monthNum = 11;
}
else
{
monthNum = 12;
}
}
//METHODS
public void setMonthNum(int monthNum)
{
this.monthNum = monthNum;
if ((monthNum >12) || (monthNum<1))
{
monthNum = 1;
}
}
public int getMonthNumber()
{
return monthNum;
}
public String getMonthName()
{
if (monthNum == 1)
{
return "January";
}
else if (monthNum == 2)
{
return "February";
}
else if (monthNum == 3)
{
return "March";
}
else if (monthNum == 4)
{
return "April";
}
else if (monthNum == 5)
{
return "May";
}
else if (monthNum == 6)
{
return "June";
}
else if (monthNum == 7)
{
return "July";
}
else if (monthNum == 8)
{
return "August";
}
else if (monthNum == 9)
{
return "September";
}
else if (monthNum == 10)
{
return "October";
}
else if (monthNum == 11)
{
return "November";
}
else
{
return "December";
}
}
public String toString()
{
if (monthNum == 1)
{
return "January";
}
else if (monthNum == 2)
{
return "February";
}
else if (monthNum == 3)
{
return "March";
}
else if (monthNum == 4)
{
return "April";
}
else if (monthNum == 5)
{
return "May";
}
else if (monthNum == 6)
{
return "June";
}
else if (monthNum == 7)
{
return "July";
}
else if (monthNum == 8)
{
return "August";
}
else if (monthNum == 9)
{
return "September";
}
else if (monthNum == 10)
{
return "October";
}
else if (monthNum == 11)
{
return "November";
}
else
{
return "December";
}
}
}
我错过了什么或是这是我这个级别的最好方法吗?
Am I missing something or is this the best way at my level to do this?
推荐答案
如果你必须自己实现Month类,那么看看枚举,否则
查看 java.time.Month - 尝试这样的事情......
If you must implement a Month class yourself then look into enums, otherwise check out the docs for java.time.Month – Try something like this...
import java.time.Month;
public Month getMonthName( int monthNum )
{
return Month.of( monthNum );
}
或者只是......
Or simply...
System.out.println( Month.of( monthNumber ) );
这篇关于我必须用Java创建一个月级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文