java - 如何单独访问字符串中的两种不同数据类型?
问题描述
因此,有关更多背景信息,您可以查看我的上一篇文章,但我觉得我问这个问题完全错了。
我得到的是一行上的一个输入:月日,例如 4 月 11 日
我需要做的是,根据给定的月份和日期,确定季节。所以我需要先得到 String inputMonth; 并获取 int inputDay;来自提供两者的单个字符串。我很方便地提供了季节的月份和日期,这导致我写了以下内容:
//I know that the inputDay is wrong
inputMonth = scnr.next();
inputDay = scnr.nextInt();
if( ((inputMonth == "March") || (inputMonth == "march")) && ((inputDay >= 20) && (inputDay <= 31))){
System.out.println("Spring");
}
else if( ((inputMonth == "April") || (inputMonth == "april")) && ((inputDay >= 1) && (inputDay <= 30))){
System.out.println("Spring");
}
else if( ((inputMonth == "May") || (inputMonth == "may")) && ((inputDay >= 1) && (inputDay <= 31))){
System.out.println("Spring");
}
else if( ((inputMonth == "June") || (inputMonth == "june")) && ((inputDay >= 1) && (inputDay <= 20))){
System.out.println("Spring");
}
else if( ((inputMonth == "June") || (inputMonth == "june")) && ((inputDay >= 21) && (inputDay <= 30))){
System.out.println("Summer");
}
else if( ((inputMonth == "July") || (inputMonth == "july")) && ((inputDay >= 1) && (inputDay <= 31))){
System.out.println("Summer");
}
else if( ((inputMonth == "August") || (inputMonth == "august")) && ((inputDay >= 1) && (inputDay <= 31))){
System.out.println("Summer");
}
else if( ((inputMonth == "September") || (inputMonth == "september")) && ((inputDay >= 1) && (inputDay <= 21))){
System.out.println("Summer");
}
else if( ((inputMonth == "September") || (inputMonth == "september")) && ((inputDay >= 22) && (inputDay <= 30))){
System.out.println("Autumn");
}
else if( ((inputMonth == "October") || (inputMonth == "october")) && ((inputDay >= 1) && (inputDay <= 31))){
System.out.println("Autumn");
}
else if( ((inputMonth == "November") || (inputMonth == "november")) && ((inputDay >= 22) && (inputDay <= 30))){
System.out.println("Autumn");
}
else if( ((inputMonth == "December") || (inputMonth == "december")) && ((inputDay >= 1) && (inputDay <= 20))){
System.out.println("Autumn");
}
else if( ((inputMonth == "December") || (inputMonth == "december")) && ((inputDay >= 21) && (inputDay <= 31))){
System.out.println("Winter");
}
else if( ((inputMonth == "January") || (inputMonth == "january")) && ((inputDay >= 1) && (inputDay <= 31))){
System.out.println("Winter");
}
else if( ((inputMonth == "February") || (inputMonth == "february")) && ((inputDay >= 1) && (inputDay <= 29))){
System.out.println("Winter");
}
else if( ((inputMonth == "March") || (inputMonth == "march")) && ((inputDay >= 1) && (inputDay <= 19))){
System.out.println("Winter");
}
else{
System.out.println("Invalid");
}
我的问题是我无法弄清楚如何从那个输入中单独确定月份和日期。每当我运行它时,我总是最终得到“无效”。
我上一篇文章中的一些人展示了不同的方法来解决这个问题,但老实说我还不熟悉数组(这是下一课)。
提前谢谢各位。
解决方案
推荐阅读
- python - Make Soup Not Getting All Data
- python - Faster way to perform sort by index on pandas group
- git - Git 工作流问题:git push、git reset --soft HEAD~1 以及如何将更新的代码推送到新分支?
- jmeter - How to properly set a correct User-Agent in JMeter
- javascript - How to mutate a array on state in a redux reducer?
- java - 试图找到 2 个 3 位数字的最高回文乘积
- html - github 页面在所有页面中显示相同的 repo
- asp.net-mvc - [DataType(DataType.Currency)] 是否四舍五入?
- javascript - 在js中获取原始样式表作为字符串?
- vba - VBA 中的“需要对象”错误,但我使用 Set 来分配对象