如果语句在 7 小时内出现问题 JAVA 闰年 [关闭]
Posted
技术标签:
【中文标题】如果语句在 7 小时内出现问题 JAVA 闰年 [关闭]【英文标题】:If statement troubles in 7 hours JAVA Leap Year [closed] 【发布时间】:2015-02-22 02:27:45 【问题描述】:我进入 Java 课程 4 周,经验为 0。 我被要求创建一个程序,该程序要求输入月份的数字表示示例 1 将是一月,也是一年的输入,并让程序输出 显示月份 如何一个月中有很多天和如果是闰年。这是我缩短的许多尝试之一。我做错了什么,它从 1 月开始,输入无关紧要。
package leapmonth;
import javax.swing.JOptionPane;
public class LeapMonth
public static void main(String[] args)
int Year;
int MonthNumber;
String MonthString;
JOptionPane.showMessageDialog(null, " HELLO, WELCOME TO LEAPMONTH ");
MonthString = JOptionPane.showInputDialog( " Please enter the numerical respresentation of the month");
MonthNumber = Integer.parseInt(MonthString);
MonthString = JOptionPane.showInputDialog( " Please enter the year");
Year = Integer.parseInt(MonthString);
if( MonthNumber == 1 && Year % 4 == 0 && Year % 100 != 0 || Year % 400 == 0)
JOptionPane.showMessageDialog(null,"The month is January. " + "January has 31 days in it" + " and is also a leap year ");
else if( MonthNumber == 1 && Year % 4 != 0 && Year % 100 == 0 || Year % 400 != 0)
JOptionPane.showMessageDialog(null,"The month is January. " + "January has 31 days in it" + " and it is not a leap year ");
else if( MonthNumber == 2 && Year % 4 == 0 && Year % 100 != 0 || Year % 400 ==0)
JOptionPane.showMessageDialog(null,"The month is February. " + "February has 29 days in it because this is a leap year ");
else if( MonthNumber == 2 && Year % 4 != 0 && Year % 100 == 0 || Year % 400 != 0)
JOptionPane.showMessageDialog(null,"The month is February. " + "February has 28 days in it because this is not a leap year ");
【问题讨论】:
【参考方案1】:这并不意味着你认为它意味着:
MonthNumber == 1 && Year % 4 == 0 && Year % 100 != 0 || Year % 400 == 0
我想你想要的是这样的:
MonthNumber == 1 && Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0)
强调括号。
解释:||
和 &&
运算符具有相同的优先级,因此它们从左到右关联。
如果没有括号,你的逻辑是有缺陷的,因为任何年份是 400 的倍数都会产生 true
。
例如,假设Year=2000
和MonthNumber=25
然后
MonthNumber == 1 && Year % 4 == 0 && Year % 100 != 0 || Year % 400 == 0
评估为
false && true && false || true
一步一步,然后计算为
((false && true) && false) || true
(false && false) || true
false || true
true
在该示例中,只要最后一个条件产生true
,那么整个表达式也将产生true
。我想这不是你想要的。
注意:我没有准确地花时间来完全验证您的逻辑。我只是发现了一个简单的错误,但可能还有其他错误。
【讨论】:
先生或女士,我已经快要哭出来试图解决这个问题了。我 30 多岁,有 18 岁的学生,这是全新的。非常感谢谢谢谢谢谢谢你 正确的结果,但错误的推理。 Java&&
的优先级高于||
;请参阅 docs.oracle.com/javase/specs/jls/se8/html/jls-15.html 中的语法规则。事实上,我所知道的所有 HLL 都在 'or' 之上有 'and',除了那些根本没有优先级(例如 APL、Forth、LISP)的 HLL,可能是因为它是 @paulo 引用的数学约定。在这种情况下,||
的右操作数没有(进一步的)&&
,因此结果是相同的。此外(如 C/C++ 等)&&
||
但不是 &
|
等是“短路”;如果不需要确定结果,则不会评估它们的操作数根本。 ...
... 在这种简单计算的情况下,这无关紧要,但结构相似的 month==2 && countFilesAndEraseThem()>0
只会在二月份删除您的文件,因为在其他月份,&&
运算符的结果是已知为假,因为无论右操作数可能产生什么,左操作数都会产生假。【参考方案2】:
您的代码中有大量重复。把它分开,让事情变得更容易。你的许多变量(一般来说,不仅仅是在编程术语中)只改变一次。只有当月份是二月时,年份才重要。
String MonthString;
int Days;
bool IsLeapYear = Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0);
String Leap = "";
if (!IsLeapYear)
Leap = "not ";
if (MonthNumber == 1)
MonthString = "January";
Days = 31;
else if (MonthNumber == 2)
MonthString = "February";
if (IsLeapYear)
Days = 29;
else
Days = 28;
else if (MonthNumber == 3)
MonthString = "March";
Days = 31;
// etc....
JOptionPane.showMessageDialog(null, "The month is " + MonthString + ". " +
MonthString + " has " + Days + " days in it and it is " + Leap +
"a leap year.");
【讨论】:
由于下雪,我的课程已连续几周取消。我正在通过电子邮件收到作业,但我真的不知道自己在做什么。 真可惜。你能做的最好的事情,就是让事情变得简单。如果您对我在这里所做的事情有任何疑问,请告诉我。 我有很多问题。超级初学者充其量用伪代码思考。不知道。如果用户输入无效输入,我的程序运行良好,直到我尝试添加一个 do while。要么得到无效的输入无限循环,要么在第一次进入时说无效,无论什么都正常工作......不真正理解你们输入的东西。但我同意我的重复和复杂 我肯定会向您的教授发送一封电子邮件,要求您澄清您不确定的任何事情。如果您在 do/while 部分需要帮助,请在此处创建另一个问题。根据我给出的答案,我只推断了这些 if 语句的变化。就像我说的,保持简单。只有变化的东西(月份名称和月份中的天数)被赋予变量。然后在它填充这些变量之后,它只在一个地方填充字符串,以防万一必须改变。【参考方案3】:iFytil's answer 恰到好处。 这只是boolean algebra laws 的问题。我建议您阅读并了解基础知识。 这将有助于将来实现复杂条件,尤其是那些具有多个否定的条件。
此外,您的代码有些过于复杂。我不确定你的作业中是否有任何规则,但如果你分开处理这些事情会更干净。 与其同时评估两件事(哪个月和闰年),导致 12 x 2 条件,一个好的方法是将它分成两部分,导致 12 + 2 条件(如果在 2 月内有一点) .此外,当您有两个以上的比较时,switch-case 结构的可读性要高得多。
让我解释一下:
String answer;
switch (MonthNumber)
case 1:
answer = "The month is January. January has 31 days in it.";
break;
case 2:
answer = "The month is February. ";
if (Year % 4 == 0 && !(Year % 100 == 0 && Year % 400 != 0))
// years divisible by 4, except those divisible by 100 and not by 400
answer += "February has 29 days in it because this is a leap-year.";
else
answer += "February has 28 days in it because this is not a leap-year.";
break;
case 3:
... // you get the idea
然后,紧随其后,解析闰年文本。
if (MonthNumber != 2) // you have already completed the text in Feb
if (Year % 4 == 0 && !(Year % 100 == 0 && Year % 400 != 0))
answer += "and is also a leap-year.";
else
answer += "and it is not a leap-year.";
输出文本中的一个小变化将消除检查是否是二月的需要,但这只是一个技巧。
【讨论】:
【参考方案4】:可以这样简化。
package leapmonth;
import javax.swing.JOptionPane;
public class LeapMonth
public static void main(String[] args)
String MonthInput;
int YearInput;
String[] MonthArray = "January","February","March"
,"April","May","June"
,"July","August","September"
,"October","November","December"
;
// array always started from 0 then January should be 0
JOptionPane.showMessageDialog(null, " HELLO, WELCOME TO LEAPMONTH ");
Input = JOptionPane.showInputDialog( "Please enter the numerical respresentation of the month : ");
MonthInput = Integer.parseInt(Input);
Input = JOptionPane.showInputDialog( "Please enter the year : ");
YearInput = Integer.parseInt(Input);
if( MonthInput != 2 && Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0))
JOptionPane.showMessageDialog(null,"The month is " + MonthArray[MonthInput+1] + ". " + MonthArray[MonthInput+1] + " has 31 days in it" + " and is also a leap year ");
else if( MonthInput != 2 && Year % 4 != 0 && (Year % 100 == 0 || Year % 400 != 0))
JOptionPane.showMessageDialog(null,"The month is " + MonthArray[MonthInput+1] + ". " + MonthArray[MonthInput+1] + " has 31 days in it" + " and it is not a leap year ");
else if( MonthInput == 2 && Year % 4 == 0 && (Year % 100 != 0 || Year % 400 ==0))
JOptionPane.showMessageDialog(null,"The month is " + MonthArray[MonthInput+1] + ". " + MonthArray[MonthInput+1] + " has 29 days in it because this is a leap year ");
else if( MonthInput == 2 && Year % 4 != 0 && (Year % 100 == 0 || Year % 400 != 0))
JOptionPane.showMessageDialog(null,"The month is " + MonthArray[MonthInput+1] + ". " + MonthArray[MonthInput+1] + " has 28 days in it because this is not a leap year ");
【讨论】:
以上是关于如果语句在 7 小时内出现问题 JAVA 闰年 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章