简体   繁体   English

其他语句不打印行?

[英]Else statement not printing line?

I wrote a simple program that is supposed to display whether if a user inputted int is a leap year or not, and if so what leap year is it.我写了一个简单的程序,它应该显示用户输入的 int 是否是闰年,如果是,它是什么闰年。

During running of the program whenever a number that was not supposed to be a leap year was inputted it did not print the else statement.在程序运行期间,只要输入了一个不应该是闰年的数字,它就不会打印 else 语句。

Note: This was written in the IDE BlueJ so io was automatically imported hence why I did not import it注意:这是写在 IDE BlueJ 中的,所以 io 是自动导入的,所以我没有导入它

/**
 * Reads a user inputted integer value and determines if it is a leap year
 * Created by Oh boy I suck at this
 * 9 September 2019
 */

import java.util.Scanner;

public class LeapYear
{
    public static int getYear(String prompt)
    {
        String newLine = System.getProperty("line.separator");
        int value = 0;
        boolean flag = true;
        while (flag)
        {
            Scanner scan = new Scanner(System.in);
            System.out.println(prompt + ": ");
            try
            {
                value = scan.nextInt();
                flag = false;
            }
            catch(java.util.InputMismatchException e)
            {
                System.out.println("What you have inputed was not an int.");
                System.out.println(newLine);
            }
        }
        return value;
    }

    public static void main (String[] args)
    {
        int year = getYear("Input the year ");
        final int leapYear = year/4;
        if(year % 4 == 0){
            if(year % 100 >= 1){
                if(year % 400 >= 1){
                    System.out.println("The year inputted: " + year + " is equivilant to " + leapYear + " leap year(s).");
                }
                else{
                    System.out.println("The year inputted: " + year + " is not a leap year.");
                }
            }
        }
    }
}

Change the if block as follows and you should get the expected output:如下更改if块,您应该得到预期的 output:

if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0) {
    System.out.println("The year inputted: " + year + " is equivilant to " + leapYear + " leap year(s).");
} else {
    System.out.println("The year inputted: " + year + " is not a leap year.");
}

Let us look at your if statements, and remember the rules for leap years.让我们看看你的 if 语句,并记住闰年的规则。

Understanding leap years了解闰年

In general, a leap year is a year that is divisible by four, ie where the statement year % 4 == 0 is true.一般来说,闰年是可以被四整除的年份,即语句year % 4 == 0为真。

But: If a year is not a leap year, when it is divisible by 100. Ie year % 100 == 0 is true.但是:如果一年不是闰年,当它可以被 100 整除时。即year % 100 == 0为真。

Exception: When that is also divisible by 400, ie year % 400 == 0 is true - then we have a leap year.例外:当它也可以被 400 整除时,即year % 400 == 0为真 - 那么我们有一个闰年。

Basically: divisible by four yields a candidate - you then have to study it further to make a final decision.基本上:被四整除会产生一个候选 - 然后你必须进一步研究它才能做出最终决定。

Decision tree决策树

  1. Is it a leap year candidate?是闰年候选人吗? year % 4 == 0 Otherwise: Display "not a leap year" year % 4 == 0否则:显示“不是闰年”
  2. Is it a leap year exception (every 100 years)?是否是闰年例外(每 100 年)? year % 100 == 0 Otherwise: Display "not a leap year" year % 100 == 0否则:显示“不是闰年”
  3. Is it a leap year exception exception (every 400 years)?是否是闰年例外(每 400 年)? year % 400 == 0 True => Display "leap year", False => Display "not a leap year" year % 400 == 0 True => 显示“闰年”,False => 显示“非闰年”

Your implementation您的实施

Let's first look at your if statement.让我们先看看你的if语句。

  1. Your first if statement checks whether the year is divisible by four.您的第一个 if 语句检查年份是否可以被 4 整除。 This is so that you know whether you deal with a leap year candidate.这是为了让您知道您是否与闰年候选人打交道。 So this is correct - but you forgot to deal with the case, when the year is NOT a leap year (so here you miss a possible "is not a leap year" output)所以这是正确的 -但是当年份不是闰年时,您忘记处理这种情况(所以在这里您错过了可能的“不是闰年”输出)

  2. Now it gets a bit confusing.现在它变得有点混乱。 You check whether the year is NOT divisible by 100. If a leap year candidate is NOT divisible by 100 it IS a leap year.您检查年份是否不能被 100 整除。如果候选闰年不能被 100 整除,则它是闰年。 So you can output success, but you have to deal with the "else" case.所以你可以 output 成功,但是你必须处理“else”的情况。

  3. The third if is nested in the wrong if block.第三个 if 嵌套在错误的 if 块中。 It belongs into the else block of the parent.它属于父级的 else 块。

Hints提示

Try to get an understanding on how the input and the output relate to each other and try to hit every if / else.尝试了解输入和 output 是如何相互关联的,并尝试点击每个 if / else。

Whenever you write an if think about whether you need the corresponding else block.每当您编写 if 时,请考虑是否需要相应的 else 块。

If things get weird, try to "trace" your program with "breadcrumb outputs": System.out.println("1");如果事情变得奇怪,请尝试使用“面包屑输出”“跟踪”您的程序: System.out.println("1"); System.out.println("2"); System.out.println("2"); ... on every line where you deal with branching or looping (if,else,switch,while...) this will trace every step the program does on the command line. ...在您处理分支或循环的每一行(if,else,switch,while ...),这将跟踪程序在命令行上执行的每一步。 You can use this until your school encourages you to use a proper IDE.您可以使用它,直到您的学校鼓励您使用合适的 IDE。

Don't forget: Practice makes perfect;)不要忘记:熟能生巧;)

Pseude code (spoiler)伪代码(剧透)

Only use this if you're completely stuck.仅当您完全卡住时才使用它。

if (year % 4 == 0) { // this might be a leap year - we need to dig further if (year % 100 == 0) { if (year % 400 == 0) { print "leap year" } else { print "not a leap year" } } else { print "leap year" } } else { print "not a leap year" } if (year % 4 == 0) { // this might be a leap year - we need to dig further if (year % 100 == 0) { if (year % 400 == 0) { print "leap year" } else { print "not a leap year" } } else { print "leap year" } } else { print "not a leap year" }

Bottom line底线

Some of your logic is incorrect and you forget to implement the else branches.您的某些逻辑不正确,您忘记实现else分支。

HTH高温高压

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

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