gpt4 book ai didi

java - 在十二生肖确定器中接收有效输入的无效 I/O

转载 作者:行者123 更新时间:2023-12-02 13:17:24 24 4
gpt4 key购买 nike

我是一名初学者编码器,我正在开发一个确定星座的项目。

问题:“else if”日期根据用户输入的月份和日期准确确定星座运势,但对于“if” ” 陈述例如五月双子座日期。对于 5 月 21 日至 5 月 31 日之间输入的任何日期,我都收到无效输入。我已尝试重写该案例,但仍然存在问题。

如果有人能看到我看不到的东西并让我知道,我将非常感激。谢谢!

switch (month) {

case 1:
if (month == 12 && day >= 22 && day <= 31)
zodiac = "Capricorn";
else if (month == 01 && day < 20)
zodiac = "Capricorn";
else
zodiac = "Invalid Input";
break;
case 2:
if (month == 1 && day >= 20 && day < 32)
zodiac = "Aquarius";
else if (month == 2 && day < 19)
zodiac = "Aquarius";
else
zodiac = "Invalid Input";
break;
case 3:
if (month == 2 && day >= 19 && day <= 29)
zodiac = "Pisces";
else if (month == 3 && day < 21)
zodiac = "Pisces";
else
zodiac = "Invalid Input";
break;
case 4:
if (month == 3 && day >= 21 && day < 32)
zodiac = "Aries";
else if (month == 4 && day < 20)
zodiac = "Aries";
else
zodiac = "Invalid Input";
break;
case 5:
if (month == 4 && day >= 20 && day < 31)
zodiac = "Taurus";
else if (month == 5 && day < 21)
zodiac = "Taurus";
else
zodiac = "Invalid Input";
break;
case 6:
if (month == 5 && day >= 21 && day < 32)
zodiac = "Gemini";
else if (month == 6 && day < 21)
zodiac = "Gemini";
else
zodiac = "Invalid Input";
break;
case 7:
if (month == 6 && day >= 21 && day < 31)
zodiac = "Cancer";
else if (month == 7 && day < 23)
zodiac = "Cancer";
else
zodiac = "Invalid Input";
break;
case 8:
if (month == 7 && day >= 23 && day < 32)
zodiac = "Leo";
else if (month == 8 && day < 23)
zodiac = "Leo";
else
zodiac = "Invalid Input";
break;
case 9:
if (month == 8 && day >= 23 && day < 32)
zodiac = "Virgo";
else if (month == 9 && day < 23)
zodiac = "Virgo";
else
zodiac = "Invalid Input";
break;
case 10:
if (month == 9 && day >= 23 && day < 31)
zodiac = "Libra";
else if (month == 10 && day < 23)
zodiac = "Libra";
else
zodiac = "Invalid Input";
break;
case 11:
if (month == 10 && day >= 23 && day < 32)
zodiac = "Scorpio";
else if (month == 11 && day < 22)
zodiac = "Scorpio";
else
zodiac = "Invalid Input";
break;
case 12:
if (month == 11 && day >= 22 && day < 31)
zodiac = "Sagittarius";
else if (month == 12 && day < 22)
zodiac = "Sagittarius";
else
zodiac = "Invalid Input";
break;

最佳答案

在您的 switch 语句中,已根据您设置的情况完成了当月的检查。在情况 1 中,月份为 1,依此类推...

我建议您将 switch 语句中的逻辑重写为如下所示:

switch (month) {

case 1: // --> month == 1 here
if (day < 20) {
zodiac = "Capricorn";
} else if (day >= 20 && day <=31) {
zodiac = "Aquarius";
}
case 2: // --> month == 2 here
if (day < 19) {
zodiac = "Aquarius";
} else if (day >= 19 && day <= 29)
zodiac = "Pisces";
break;

// etc
}

关于java - 在十二生肖确定器中接收有效输入的无效 I/O,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43717240/

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