Java 形成这个if语句的更好方法是什么?
我目前正在从一本书中学习java,一个项目是在输入月数后输出一个月的天数和月名。我想知道是否有比我已经做的更好的方法来设置我的if语句 PS:ConsoleReader只是一个包含的类,可以轻松地从用户控制台获取输入Java 形成这个if语句的更好方法是什么?,java,if-statement,system.out,Java,If Statement,System.out,我目前正在从一本书中学习java,一个项目是在输入月数后输出一个月的天数和月名。我想知道是否有比我已经做的更好的方法来设置我的if语句 PS:ConsoleReader只是一个包含的类,可以轻松地从用户控制台获取输入 public class Project13 { public static void main(String[] args) { ConsoleReader console = new ConsoleReader(System.in); System.out.
public class Project13 {
public static void main(String[] args) {
ConsoleReader console = new ConsoleReader(System.in);
System.out.println("Enter a month you would like to evaluate (by number):");
int month = console.readInt();
int days = 0;
String monthout = "Month";
String out = "Yes";
if(month == 1){
days = 31;
monthout = "January";
out = "There are " + days + " days in " + monthout;
}else if(month == 2){
System.out.println("Is it a leap year? Yes or No:");
String leap = console.readLine();
if(leap.equalsIgnoreCase("yes")){
days = 29;
monthout = "February";
out = "There are " + days + " days in " + monthout;
}else if(leap.equalsIgnoreCase("no")){
days = 28;
monthout = "February";
out = "There are " + days + " days in " + monthout;
}else{
out = "Something went wrong, please try again";
}
}else if(month == 3){
days = 31;
monthout = "March";
out = "There are " + days + " days in " + monthout;
}else if(month == 4){
days = 30;
monthout= "April";
out = "There are " + days + " days in " + monthout;
}else if(month == 5){
days = 31;
monthout = "May";
out = "There are " + days + " days in " + monthout;
}else if(month == 6){
days = 30;
monthout = "June";
out = "There are " + days + " days in " + monthout;
}else if(month == 7){
days = 31;
monthout = "July";
out = "There are " + days + " days in " + monthout;
}else if(month == 8){
days = 31;
monthout = "August";
out = "There are " + days + " days in " + monthout;
}else if(month == 9){
days = 30;
monthout = "September";
out = "There are " + days + " days in " + monthout;
}else if(month == 10){
days = 31;
monthout = "October";
out = "There are " + days + " days in " + monthout;
}else if(month == 11){
days = 30;
monthout = "November";
out = "There are " + days + " days in " + monthout;
}else if(month == 12){
days = 31;
monthout = "December";
out = "There are " + days + " days in " + monthout;
}else if(month > 12){
out = "Your month input was not valid. Please try again.";
}
System.out.println(out);
}
}
如果你要这样做,你肯定应该使用enum
,而不仅仅是表示月份的整数
如果你要这样做,你一定要使用
枚举
,而不仅仅是表示月份的整数…你可以使用开关大小写
语句。避免使用太多if-else语句
switch(month) {
case 1:
days = 31;
monthout = "January";
out = "There are " + days + " days in " + monthout;
break;
case 2:
// Add stuff
break;
default:
break;
}
你可以使用
开关盒语句。如果没有其他语句,请避免使用太多
switch(month) {
case 1:
days = 31;
monthout = "January";
out = "There are " + days + " days in " + monthout;
break;
case 2:
// Add stuff
break;
default:
break;
}
Swtich:
switch (month)
{
case 1:
// ... stuff here
break;
case 2:
// ... stuff here
break;
// ... more cases
default: // no case above matched what the month was
// .... handle default case
};
Swtich:
switch (month)
{
case 1:
// ... stuff here
break;
case 2:
// ... stuff here
break;
// ... more cases
default: // no case above matched what the month was
// .... handle default case
};
如果需要,请使用switch语句而不是其他许多语句。它更容易阅读,修改和休息的工作速度比其他如果
case 1:
//code
break;
如果需要,请使用switch语句而不是其他许多语句。它更容易阅读,修改和休息的工作速度比其他如果
case 1:
//code
break;
您可以用一对数组替换几乎整个if
语句,如下所示:
int dayCount[] = new int[] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
String monthName[] = new String[] {"January", "February", ...};
有了这两个阵列,您可以执行以下操作:
// February is the only month that needs special handling
if (month == 2) {
// Do your special handling of leap year etc...
} else if (month >= 1 && month <= 12) {
// All other valid months go here. Since Java arrays are zero-based,
// we subtract 1 from the month number
days = dayCount[month-1];
monthout = monthName[month-1];
} else {
// Put handling of invalid month here
}
out = "There are " + days + " days in " + monthout;
//二月是唯一需要特殊处理的月份
如果(月份==2){
//你对闰年等的特殊处理。。。
}else if(month>=1&&month您可以用一对数组替换几乎整个if
语句,如下所示:
int dayCount[] = new int[] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
String monthName[] = new String[] {"January", "February", ...};
有了这两个阵列,您可以执行以下操作:
// February is the only month that needs special handling
if (month == 2) {
// Do your special handling of leap year etc...
} else if (month >= 1 && month <= 12) {
// All other valid months go here. Since Java arrays are zero-based,
// we subtract 1 from the month number
days = dayCount[month-1];
monthout = monthName[month-1];
} else {
// Put handling of invalid month here
}
out = "There are " + days + " days in " + monthout;
//二月是唯一需要特殊处理的月份
如果(月份==2){
//你对闰年等的特殊处理。。。
}else if(month>=1&&month我认为您应该使用Map
来避免所有这些if(或switch)情况。代码的主要问题是您的代码重复了很多代码
private static Map<Integer, Integer> monthDays = new HashMap<>();
static {
monthDays.put(1, 31);
monthDays.put(2, 28);
...
}
private static int getMonthDays(int month) {
if (month == 2) {
// handle special case with Februar
}
return monthDays.get(month);
}
public static void main(String[] args) {
...
if (month >= 1 && month <= 12) {
monthout = getMonthDays(month);
...
out = "There are " + days + " days in " + monthout;
} else {
out = "Your month input was not valid. Please try again.";
}
System.out.println(out);
}
private static Map monthDays=new HashMap();
静止的{
月日.投入产出(1,31);
月日。投入产出(2,28);
...
}
私有静态整数getMonthDays(整数月){
如果(月份==2){
//2月处理特殊情况
}
返回monthDays.get(月);
}
公共静态void main(字符串[]args){
...
if(month>=1&&month我认为您应该使用Map
来避免所有这些if(或switch)情况。代码的主要问题是您的代码重复了很多代码
private static Map<Integer, Integer> monthDays = new HashMap<>();
static {
monthDays.put(1, 31);
monthDays.put(2, 28);
...
}
private static int getMonthDays(int month) {
if (month == 2) {
// handle special case with Februar
}
return monthDays.get(month);
}
public static void main(String[] args) {
...
if (month >= 1 && month <= 12) {
monthout = getMonthDays(month);
...
out = "There are " + days + " days in " + monthout;
} else {
out = "Your month input was not valid. Please try again.";
}
System.out.println(out);
}
private static Map monthDays=new HashMap();
静止的{
月日.投入产出(1,31);
月日。投入产出(2,28);
...
}
私有静态整数getMonthDays(整数月){
如果(月份==2){
//2月处理特殊情况
}
返回monthDays.get(月);
}
公共静态void main(字符串[]args){
...
if(month>=1&&month切换情况也不好。如果超过3-4 if else或切换条件,请考虑其他方式,如命令或策略模式或其他方式
例如,创建一个动作图,在if条件下设置并执行您想要的动作。示例代码如下。这只是示例代码,如果您需要对任何部分进行进一步细化,请告知我:
public void initMap(){
Map<Integer, MonthData> monthDataMap = new HashMap<Integer,MonthData>();
monthDataMap.put(1,new JanData());
monthDataMap.put(2,new FebData());
}
interface MonthData {
public String getMonth();
public int getDays();
public String getOut();
}
class JanData implements MonthData{
private String month ="January";
private int days = 30;
private String out = "There are " + days + " days in " + month;
@Override
public String getMonth() {
return month;
}
@Override
public int getDays() {
return days;
}
@Override
public String getOut() {
return out;
}
}
class FebData {
private String month ="February";
private int days = 28;
private String out = "There are " + days + " days in " + month;
}
....
public void initMap(){
Map monthDataMap=新HashMap();
monthDataMap.put(1,new JanData());
monthDataMap.put(2,新的FebData());
}
接口MonthData{
公共字符串getMonth();
public int getDays();
公共字符串getOut();
}
类JanData实现MonthData{
私有字符串month=“一月”;
私人整数天=30天;
private String out=“在“+月份中有“+天+”天;
@凌驾
公共字符串getMonth(){
返回月份;
}
@凌驾
公共整数getDays(){
返程天数;
}
@凌驾
公共字符串getOut(){
返回;
}
}
类FebData{
私有字符串month=“二月”;
私人整数天=28天;
private String out=“在“+月份中有“+天+”天;
}
....
切换情况也不好。如果有超过3-4个If else或切换条件,请考虑其他方式,如命令或策略模式或其他方式
例如,创建一个动作图,在if条件下设置并执行您想要的动作。示例代码如下。这只是示例代码,如果您需要对任何部分进行进一步细化,请告知我:
public void initMap(){
Map<Integer, MonthData> monthDataMap = new HashMap<Integer,MonthData>();
monthDataMap.put(1,new JanData());
monthDataMap.put(2,new FebData());
}
interface MonthData {
public String getMonth();
public int getDays();
public String getOut();
}
class JanData implements MonthData{
private String month ="January";
private int days = 30;
private String out = "There are " + days + " days in " + month;
@Override
public String getMonth() {
return month;
}
@Override
public int getDays() {
return days;
}
@Override
public String getOut() {
return out;
}
}
class FebData {
private String month ="February";
private int days = 28;
private String out = "There are " + days + " days in " + month;
}
....
public void initMap(){
Map monthDataMap=新HashMap();
monthDataMap.put(1,new JanData());
monthDataMap.put(2,新的FebData());
}
接口MonthData{
公共字符串getMonth();
public int getDays();
公共字符串getOut();
}
类JanData实现MonthData{
私有字符串month=“一月”;
私人整数天=30天;
private String out=“在“+月份中有“+天+”天;
@凌驾
公共字符串getMonth(){
返回月份;
}
@凌驾
公共整数getDays(){
返程天数;
}
@凌驾
公共字符串getOut(){
返回;
}
}
类FebData{
私有字符串month=“二月”;
私人整数天=28天;
private String out=“在“+月份中有“+天+”天;
}
....
不一定更好,但您可以尝试切换case
语句:不一定更好,但您可以尝试切换case
语句:+1。这是唯一的答案,它不仅用过程切换语句的集合替换过程if语句的集合,而且使用合理的编程pr这是唯一的答案,它不仅用一组过程切换语句代替了一组过程if语句,而且使用了合理的编程原则。