简体   繁体   English

Java条件语句(足够简单)

[英]Java Conditional statement (simple enough)

My program is to tell if a year entered is a leap year that meets these requirements: 我的程序是告诉输入的年份是否是满足以下要求的a年:

A year is a leap year if it is divisible by 4. But if the year is divisible by 100, it is a leap year only when it is also divisible by 400. 如果年份可以被4整除,则年份是a年。但是如果年份可以被100整除,则仅当年份也可以被400整除时,它才是a年。

Create a program that checks whether the given year is a leap year. 创建一个检查给定年份是否为whether年的程序。

Here are what results should be produced per input: 以下是每个输入应产生的结果:

Type a year: 2011 The year is not a leap year. 输入年份:2011年不是a年。

Type a year: 2012 The year is a leap year. 输入年份:2012年是a年。

Type a year: 1800 The year is not a leap year. 输入年份:1800年不是a年。

Type a year: 2000 The year is a leap year. 输入年份:2000年是a年。

This is what I came up with: 这是我想出的:

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");
        } 
    }
}  

All of them work except 1800. 1800 is not a leap year and my program says that it is (it is not divisible by 400). 除1800以外,其他所有参数都起作用。1800不是a年,而我的程序说是(年(不能被400整除)。 It seems that (number % 100 == 0 && number % 400 == 0) only works if (number % 4 == 0 ) is not there. 似乎(number%100 == 0 && number%400 == 0)仅在(number%4 == 0)不存在时才起作用。 Why does my program not work correctly? 为什么我的程序无法正常运行?

Consider what happens when the year 100 is inputted. 考虑输入100年时发生的情况。 100 % 4 ==0 is TRUE , so your code outputs that the year is a leap year. 100 % 4 ==0TRUE ,因此您的代码输出年份为a年。 The problem is, 100 % 100 == 0 is also TRUE , but your code will never reach this line, nor will it check if 100 % 400 == 0 is TRUE . 问题是, 100 % 100 == 0也为TRUE ,但是您的代码将永远不会到达此行,也不会检查100 % 400 == 0是否为TRUE

You printed out the result before you checked all of the conditions! 在检查所有条件之前,您已打印出结果!

Change the structure of your if else. 如果不是,请更改您的结构。

This sounds like a homework assignment, so I don't want to give you the answer. 这听起来像是一项家庭作业,所以我不想给你答案。 You should have all of the information needed to reach it from here. 您应该具有从此处获取所需的所有信息。 If not, feel free to comment with any questions. 如果没有,请随时提出任何问题。

EDIT: Since you appear to have gotten the core of the problem, here is what is wrong with your answer. 编辑:由于您似乎已经掌握了问题的核心,因此您的答案有误。

Your brackets { and } are in the wrong places, among other things. 除其他外,方括号{}放在错误的位置。 Placing your brackets in the locations I do is part of the Java standard, and makes your code easier to read, understand, and debug. 将括号放在我的位置是Java标准的一部分,使您的代码更易于阅读,理解和调试。 (It makes it easier to identify when they are missing as well.) (这也使他们更容易识别何时丢失。)

Your code should look something like this: 您的代码应如下所示:

// 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");
} 

Try this 尝试这个

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

That should work for you. 那应该为您工作。 You're preempting your own logic by outputting early. 您会通过提早输出来抢占自己的逻辑。

    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");
    } 
}

} }

Here you go HC. 在这里,你去HC。 I only cannot get it to print properly. 我只能使它无法正确打印。 Close? 关?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM