Java 条件语句(足够简单)
Java Conditional statement (simple enough)
我的程序是判断输入的年份是否为满足这些要求的闰年:
能被4整除的年份是闰年,能被100整除的年份只有能被400整除的才是闰年
创建一个程序来检查给定年份是否为闰年。
以下是每个输入应产生的结果:
输入年份:2011
年份不是闰年。
输入年份:2012
那年是闰年。
输入年份:1800
年份不是闰年。
输入年份:2000
那年是闰年。
这是我想出的:
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println(" Type a year ");
int number = Integer.parseInt(reader.nextLine());
if (number % 4 == 0 ) {
System.out.println(" This is a leap year");
} else if (number % 100 == 0 && number % 400 == 0) {
System.out.println(" This is a leap year ");
} else {
System.out.println( "This is not a leap year");
}
}
}
除 1800 外,所有这些都有效。1800 不是闰年,我的程序说是(它不能被 400 整除)。似乎 (number % 100 == 0 && number % 400 == 0) 只有在 (number % 4 == 0) 不存在的情况下才有效。为什么我的程序不能正常运行?
试试这个
bool isLeap = false;
if (number % 4 == 0) {
isLeap = true;
if (number % 100 == 0)
isLeap = false;
if (number % 400 == 0)
isLeap = true;
}
if (isLeap) { //print stuff
这应该适合你。您正在通过提前输出来抢占您自己的逻辑。
考虑输入 100 年时会发生什么。 100 % 4 ==0
是 TRUE
,所以你的代码输出的年份是闰年。问题是,100 % 100 == 0
也是 TRUE
,但是你的代码永远不会到达这一行,也不会检查 100 % 400 == 0
是否是 TRUE
。
您在检查所有条件之前打印出结果!
更改 if else 的结构。
这听起来像是家庭作业,所以我不想给你答案。您应该拥有从这里到达它所需的所有信息。如果没有,请随时发表任何问题。
编辑:由于您似乎已经找到了问题的核心,所以您的回答有问题。
你的方括号 {
和 }
放错了地方等等。将你的括号放在我做的位置是 Java 标准的一部分,并使你的代码更易于阅读、理解和调试。 (这样也更容易识别它们何时丢失。)
您的代码应如下所示:
// Pay attention to a few things here. It checks if it is divisible by 4
// since every leap year must be divisible by 4. If it is,
// it checks if it is divisible by 100. If it is, it must also
// be divisible by 400, or it is not a leap year. So, if it is divisible
// by 100 and NOT divisible by 400, it is not a leap year. If it isn't
// divisible by 100, control flows to the else statement, and since we
// already tested number % 4 we know it is a leap year.
// Pay special attention to where I located my { and }, this is the
// standard way to do it in java, it makes your code readable by others.
if(number % 4 == 0) {
if((number % 100 == 0) && !(number % 400 == 0)) { // NOTE THE ! OPERATOR HERE
System.out.println("The year is NOT a leap year.");
} else {
System.our.println("The year is a leap year.");
}
} else {
System.out.println("The year is NOT a leap year");
}
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println(" Type a year ");
int number = Integer.parseInt(reader.nextLine());
if (number % 4 == 0 )
if (number % 100 == 0)
if (number % 400 == 0)
{
System.out.println(" This is a leap year");
} else {
System.out.println( "This is not a leap year");
}
}
}
HC 给你。我只是无法正确打印它。关闭?
我的程序是判断输入的年份是否为满足这些要求的闰年:
能被4整除的年份是闰年,能被100整除的年份只有能被400整除的才是闰年
创建一个程序来检查给定年份是否为闰年。
以下是每个输入应产生的结果:
输入年份:2011 年份不是闰年。
输入年份:2012 那年是闰年。
输入年份:1800 年份不是闰年。
输入年份:2000 那年是闰年。
这是我想出的:
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println(" Type a year ");
int number = Integer.parseInt(reader.nextLine());
if (number % 4 == 0 ) {
System.out.println(" This is a leap year");
} else if (number % 100 == 0 && number % 400 == 0) {
System.out.println(" This is a leap year ");
} else {
System.out.println( "This is not a leap year");
}
}
}
除 1800 外,所有这些都有效。1800 不是闰年,我的程序说是(它不能被 400 整除)。似乎 (number % 100 == 0 && number % 400 == 0) 只有在 (number % 4 == 0) 不存在的情况下才有效。为什么我的程序不能正常运行?
试试这个
bool isLeap = false;
if (number % 4 == 0) {
isLeap = true;
if (number % 100 == 0)
isLeap = false;
if (number % 400 == 0)
isLeap = true;
}
if (isLeap) { //print stuff
这应该适合你。您正在通过提前输出来抢占您自己的逻辑。
考虑输入 100 年时会发生什么。 100 % 4 ==0
是 TRUE
,所以你的代码输出的年份是闰年。问题是,100 % 100 == 0
也是 TRUE
,但是你的代码永远不会到达这一行,也不会检查 100 % 400 == 0
是否是 TRUE
。
您在检查所有条件之前打印出结果!
更改 if else 的结构。
这听起来像是家庭作业,所以我不想给你答案。您应该拥有从这里到达它所需的所有信息。如果没有,请随时发表任何问题。
编辑:由于您似乎已经找到了问题的核心,所以您的回答有问题。
你的方括号 {
和 }
放错了地方等等。将你的括号放在我做的位置是 Java 标准的一部分,并使你的代码更易于阅读、理解和调试。 (这样也更容易识别它们何时丢失。)
您的代码应如下所示:
// Pay attention to a few things here. It checks if it is divisible by 4
// since every leap year must be divisible by 4. If it is,
// it checks if it is divisible by 100. If it is, it must also
// be divisible by 400, or it is not a leap year. So, if it is divisible
// by 100 and NOT divisible by 400, it is not a leap year. If it isn't
// divisible by 100, control flows to the else statement, and since we
// already tested number % 4 we know it is a leap year.
// Pay special attention to where I located my { and }, this is the
// standard way to do it in java, it makes your code readable by others.
if(number % 4 == 0) {
if((number % 100 == 0) && !(number % 400 == 0)) { // NOTE THE ! OPERATOR HERE
System.out.println("The year is NOT a leap year.");
} else {
System.our.println("The year is a leap year.");
}
} else {
System.out.println("The year is NOT a leap year");
}
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println(" Type a year ");
int number = Integer.parseInt(reader.nextLine());
if (number % 4 == 0 )
if (number % 100 == 0)
if (number % 400 == 0)
{
System.out.println(" This is a leap year");
} else {
System.out.println( "This is not a leap year");
}
}
}
HC 给你。我只是无法正确打印它。关闭?