这是分配。我相信我只能使用If和else语句
以下算法产生季节(春季,夏季,秋季或夏季) 冬天)一天中的某个月。
IF month is 1,2, or 3, season = "Winter Else if month is 4, 5, or 6, season = "Spring" Else if month is 7,8, 9, season = "Summer" Else if month is 10,11, or 12, season = "Fall" If month is divisible by 3 and day >= 21 If season is "Winter", season = "Spring" Else if season is "Spring", season = "Summer" Else if season is "Summer", season = "Fall" Else season = "Winter"
编写一个程序,提示用户一个月和一天然后 打印季节,由此算法确定
到目前为止我得到了什么。如果月份可以被3整除并且日期高于20,我无法得到最后一部分来改变季节。
import java.util.Scanner;
public class Seasons {
public static void main (String[] args){
Scanner in = new Scanner(System.in);
int month;
int day;
String season = null;
System.out.print("Please enter month and day: ");
month = in.nextInt();
day = in.nextInt();
if(1 <= month && month <=3){
season = "Winter";
System.out.println(season);
}
else if (4 <= month && month <=6){
season = "Spring";
System.out.println(season);
}
else if (7 <=month && month <=9){
season = "Summer";
System.out.println(season);
}
else if (10 <= month && month <= 12){
season = "Fall";
System.out.println(season);
}
if ( ((month % 3 == 0) && (21 <= day)){
if (season.equals("Winter")){
}
else if (season.equals("Spring")){
season = ("Summer");
}
else if (season.equals("Summer")){
season = "Fall";
}
else {
season = "Winter";
}
}
}
}
答案 0 :(得分:2)
根据您的条件,这样的事情应该有效:
private String getSesson(int month, int day) {
String season = "";
switch (month) {
case 1:
case 2:
case 3:
season = "Winter";
if(month % 3 == 0 && day >=21)
season = "Spring";
break;
case 4:
case 5:
case 6:
season = "Spring";
if(month % 3 == 0 && day >=21)
season = "Summer";
break;
case 7:
case 8:
case 9:
season = "Summer";
if(month % 3 == 0 && day >=21)
season = "Fall";
break;
case 10:
case 11:
case 12:
season = "Fall";
if(month % 3 == 0 && day >=21)
season = "Winter";
break;
default:
season = "Not a valid month";
break;
}
return season;
}
试验:
System.out.println(getSesson(1, 0));
System.out.println(getSesson(2, 0));
System.out.println(getSesson(3, 0));
System.out.println(getSesson(3, 21));
System.out.println(getSesson(4, 0));
System.out.println(getSesson(5, 0));
System.out.println(getSesson(6, 0));
System.out.println(getSesson(6, 21));
System.out.println(getSesson(7, 0));
System.out.println(getSesson(8, 0));
System.out.println(getSesson(9, 0));
System.out.println(getSesson(9, 21));
System.out.println(getSesson(10, 0));
System.out.println(getSesson(11, 0));
System.out.println(getSesson(12, 0));
System.out.println(getSesson(12, 21));
以上Syso打印:
Winter
Winter
Winter
Spring
Spring
Spring
Spring
Summer
Summer
Summer
Summer
Fall
Fall
Fall
Fall
Winter
如果...版本:
Scanner in = new Scanner(System.in);
int month, day; String season = null;
System.out.print("Please enter month and day: ");
month = in.nextInt(); day = in.nextInt();
if(1 <= month && month <= 3){
season = "Winter";
if((month == 3) && (21 <= day))
season = "Spring";
} else if (4 <= month && month <=6){
season = "Spring";
if((month == 6) && (21 <= day))
season = "Summer";
} else if (7 <=month && month <=9){
season = "Summer";
if((month == 9) && (21 <= day))
season = "Fall";
} else if (10 <= month && month <= 12){
season = "Fall";
if((month == 12) && (21 <= day))
season = "Winter";
}
System.out.println(season);
答案 1 :(得分:1)
为了您自己的兴趣,您可以使用
String season= "Winter,Spring,Summer,Fall".split(",")[(month + day/21 - 1)/3 &3];
for (int month = 1; month <= 12; month++)
for (int day = 20; day <= 21; day++) {
String season = "Winter,Spring,Summer,Fall".split(",")[(month + day / 21 - 1) / 3 & 3];
System.out.println(day + "/" + month + " => " + season);
}
打印
20/1 => Winter
21/1 => Winter
20/2 => Winter
21/2 => Winter
20/3 => Winter
21/3 => Spring
20/4 => Spring
21/4 => Spring
20/5 => Spring
21/5 => Spring
20/6 => Spring
21/6 => Summer
20/7 => Summer
21/7 => Summer
20/8 => Summer
21/8 => Summer
20/9 => Summer
21/9 => Fall
20/10 => Fall
21/10 => Fall
20/11 => Fall
21/11 => Fall
20/12 => Fall
21/12 => Winter
答案 2 :(得分:0)
使用模数运算符(%)来获取除法后的提醒,如下所示:
if ((month % 3) == 0 && day >= 21)
{
if (season.equals("Winter"))
{
season = "Summer";
}
// ... etc ...
}
答案 3 :(得分:0)
我认为这是来自if块中的逻辑测试,使用括号作为一个单元进行测试:
if ( ((month % 3) == 0) && (day >= 21) )
修改:
您需要做的就是将变量重新分配给新赛季。
if ( ((month % 3) == 0) && (day >= 21) ) {
season = "Winter";
}
继续在season
只要条件全部评估为true
。然后变量将更改为winter
。但如果它是false
则不会改变。
您可以尝试:
if(true) {
season = "Winter";
}
System.out.println(season);
您应该看到Winter
。
答案 4 :(得分:0)
您的原始算法看起来不对.. 如果你在第一部分确定它是冬天(第1,2或3个月)并且它是第3个月并且那天大于20,那将是春天,而不是夏天..
这是应该说的:
if (month % 3 == 0 && 21 <= day){
if (season.equals("Winter")){
season = "Spring"
}
else if (season.equals("Spring")){
season = "Summer";
}
else if (season.equals("Summer")){
season = "Fall";
}
else {
season = "Winter";
}
}
你错过了冬天的价值。
或者你可以这样做:
public class Seasons {
public static void main (String[] args){
Scanner in = new Scanner(System.in);
int month;
int day;
String season = null;
System.out.print("Please enter month and day: ");
month = in.nextInt();
day = in.nextInt();
String[] seasons = {"Winter", "Spring", "Summer", "Fall"};
if (day >= 21 && month % 3 == 0) {
season = seasons[((month + 2) / 3) % 4];
} else {
season = seasons[((month-1)/3)];
}
System.out.println("Season: " + season);
}
}
答案 5 :(得分:0)
代码上的实际问题(一般来说,有效)是您在更改之前打印季节值。这就是为什么你无法看到算法的第二部分何时起作用的原因。
删除每个if语句的System.out
,并在方法结束时仅更改它们。还要修复你的最后一个if / elses,因为冬天是空的。
最终代码:
public static void main (String[] args){
Scanner in = new Scanner(System.in);
int month;
int day;
String season = null;
System.out.print("Please enter month and day: ");
month = in.nextInt();
day = in.nextInt();
if(1 <= month && month <=3){
season = "Winter";
}
else if (4 <= month && month <=6){
season = "Spring";
}
else if (7 <=month && month <=9){
season = "Summer";
}
else if (10 <= month && month <= 12){
season = "Fall";
}
if ( ((month % 3 == 0) && (21 <= day)){
if (season.equals("Winter")){
season="Spring";
}
else if (season.equals("Spring")){
season = "Summer";
}
else if (season.equals("Summer")){
season = "Fall";
}
else {
season = "Winter";
}
}
System.out.println(season);
}
这将打印最终结果(即3月22日的“春天”)。
答案 6 :(得分:0)
import javax.swing.JOptionPane;
public class SeasonDetictor {
public static void main(String[] args) {
// TODO Auto-generated method stub
String month1 = JOptionPane.showInputDialog("please enter number of the month");
String day1 = JOptionPane.showInputDialog("please enter number of the day");
int month = Integer.parseInt(month1);
int day = Integer.parseInt(day1);
if ((month == 3)&&(day >= 21 && day <= 31) ) {
JOptionPane.showMessageDialog(null, "spring");
}
else if ((month == 4)||(month == 5)) {
JOptionPane.showMessageDialog(null, "spring");
}
else if ((month == 6) && (day < 21)) {
JOptionPane.showMessageDialog(null, "spring");
}
if ((month == 6)&&(day >= 21 && day <= 31) ) {
JOptionPane.showMessageDialog(null, "summer");
}
else if ((month == 7)||(month == 8)) {
JOptionPane.showMessageDialog(null, "summer");
}
else if ((month == 9) && (day < 21)) {
JOptionPane.showMessageDialog(null, "summer");
}
if ((month == 9)&&(day >= 21 && day <= 31) ) {
JOptionPane.showMessageDialog(null, "fall");
}
else if ((month == 10)||(month == 11)) {
JOptionPane.showMessageDialog(null, "fall");
}
else if ((month == 12) && (day < 21)) {
JOptionPane.showMessageDialog(null, "fall");
}
if ((month == 12)&&(day >= 21 && day <= 31) ) {
JOptionPane.showMessageDialog(null, "winter");
}
else if ((month == 1)||(month == 2)) {
JOptionPane.showMessageDialog(null, "winter");
}
else if ((month == 3) && (day < 21)) {
JOptionPane.showMessageDialog(null, "winter");
}
}
}