简体   繁体   English

if 和 else 语句未按预期工作

[英]if and else statements not working as intended

I am making a really simple program, mostly to learn the if/else statements.我正在制作一个非常简单的程序,主要是为了学习if/else语句。 The program is made to figure out if you have chosen a correct date.该程序旨在确定您是否选择了正确的日期。

The program is intended to work as follows:该程序旨在按以下方式工作:

  • Start the program, it gives you an instruction to type in a month using numbers.启动程序,它会提示您使用数字输入月份。
  • If you type in a number between 1 and 12, it will give you another, similar instruction, but for days.如果你输入一个 1 到 12 之间的数字,它会给你另一个类似的指令,但需要好几天。
  • If you have chosen another number, or a word, it should say "month wrong".如果你选择了另一个数字或一个词,它应该说“月错”。 Depending on which month you have chosen, you will have different numbers of days that are "correct".根据您选择的月份,您将拥有不同的“正确”天数。 If you have chosen right, it should say "Date correct".如果您选择正确,它应该显示“日期正确”。
  • If you have chosen a date that is not correct, it should say "Wrong day for the chosen month".如果您选择的日期不正确,它应该显示“所选月份的日期错误”。

The problem is, that if you type for example 15 in the month section, it states问题是,如果您在月份部分输入例如 15,它会指出

MonthWrongWrongdayinthemonthWrongdayinthemonth月错月错日月错日

instead of just而不仅仅是

month wrong月错

Is this because I have if/else statements inside of other if/else statements?这是因为我if/else其他的内声明if/else语句? I have tried searching for this everywhere but I can not figure out what's wrong..我试过到处寻找这个,但我不知道出了什么问题..

This is a link to a picture of the console when I try to run the app.这是我尝试运行应用程序时控制台图片的链接。

Picture of console控制台图片

Please excuse the Swedish words.请原谅瑞典语。

import java.util.Scanner;

public class DateChecker111 {

    public static void main (String args[]) {

        Scanner scanner1 = new Scanner(System.in);

        int Manad, Dag;
        System.out.print("Ange Månad>");

        Manad = scanner1.nextInt();
        if (Manad > 0 && Manad < 13) {
        }
        else {
            System.out.print("Felaktig Månad");
        }

        if (Manad == 1 || Manad == 3 || Manad == 5 || Manad == 7 || Manad == 8 || Manad == 10 || Manad == 12) {
            System.out.print("Ange Dag>");
            Dag = scanner1.nextInt();
            if (Dag > 0 && Dag < 32); 
            System.out.print("Korrekt Datum");
        }
        else {
                System.out.print("Felaktig Dag i Månaden");
        }

        if (Manad == 2) {
            System.out.print("Ange Dag");
            Dag = scanner1.nextInt();
            if (Dag > 0 && Dag < 29); 
            System.out.print("Korrekt Datum");
        }
        else {
            System.out.print("Felaktig Dag i Månaden");
        }

        if (Manad == 4 || Manad == 6 || Manad == 9 || Manad == 11) {
            System.out.println("Ange Dag");
            Dag = scanner1.nextInt();
            if (Dag > 0 && Dag < 31);
            System.out.print("Korrekt Datum");
        } 
        else {
            System.out.print("Felaktig Dag i Månaden");

            scanner1.close();

        }
    }


}

The real problem here, and the root of all your troubles, is that you consistently end if statements with semicolons.这里真正的问题,也是你所有麻烦的根源,是你总是以分号结束if语句。 For example, you write:例如,你写:

if (Dag > 0 && Dag < 32); 
    System.out.print("Korrekt Datum");

This is equivalent to having no if statement at all!这相当于根本没有if语句!

An if statement has the following form: if语句具有以下形式:

if (condition)
    statement

and an if-else statement has the following form:并且if-else语句具有以下形式:

if (condition)
    statement
else
    anotherStatement

Notice that I left off the semicolons and braces.请注意,我省略了分号和大括号。 That was deliberate.那是故意的。 In either of those forms, statement and anotherStatement can either be a single statement ending in a semicolon, or a Block consisting of some number of statements inside braces.在这两种形式中, statementanotherStatement可以是以分号结尾的单个语句,也可以是由大括号内的一些statements组成的Block So when you end your if statement with a semicolon, you're actually writing所以当你用分号结束if语句时,你实际上是在写

if (condition)
    ;

In the example code I quoted above, you're writing在我上面引用的示例代码中,您正在编写

if (Dag > 0 && Dag < 32)
    ; 
System.out.print("Korrekt Datum");

Take out the extra semicolons and your life will be happier.去掉多余的分号,你的生活会更快乐。

称谓的执行流程不会因为if/else的一个分支已经被执行而停止,它会继续执行第二个if/else,其结果也将被输出。

Your second if-statement gets executed regardless whether the first one succeeds or not.无论第一个是否成功,您的第二个 if 语句都会执行。 To fix that, you can add要解决这个问题,您可以添加

    return;

right after紧接着

    System.out.println("Felaktig Manad");

Also, be very careful with semicolons (;).另外,使用分号 (;) 时要非常小心。 There are multiple cases where you use a semicolon right after an if-statement which causes the if-statement to do nothing at all (it essentially gets skipped completely).在多种情况下,您在 if 语句之后使用分号会导致 if 语句根本不执行任何操作(它基本上被完全跳过)。 Replace those semicolons with closing curly brackets (}).用大括号 (}) 替换这些分号。 Depending on your IDE you can probably hit crtl+I so it indents your code automatically and you can see clearly what it does.根据您的 IDE,您可能可以按 crtl+I 以便它自动缩进您的代码,您可以清楚地看到它的作用。

Below is your code formatted better so you can see the mistakes you've made.下面是你的代码格式更好,所以你可以看到你犯的错误。 There are 3 if statements that end in ;有 3 个以;结尾的if语句; , instead of a { as you intended. , 而不是您想要的{ With the code formatted as it is now, you should notice that instead of a bunch of nested if statements, you have several if statements that will all be run, even if the first one fails.使用现在格式化的代码,您应该注意到,您有几个if语句,而不是一堆嵌套的if语句,它们将全部运行,即使第一个失败。 This is what you are seeing.这就是你所看到的。 To fix this, remove the ;要解决此问题,请删除; at the end of the if statements, and add { .if语句的末尾,并添加{ (I marked the bad ifs with // BAD IF ) (我用// BAD IF标记了错误的 ifs)

import java.util.Scanner;

public class DateChecker111 {

    public static void main (String args[]) {

        Scanner scanner1 = new Scanner(System.in);

        int Manad, Dag;
        System.out.print("Ange Månad>");

        Manad = scanner1.nextInt();
        if (Manad > 0 && Manad < 13) {
        }
        else {
            System.out.print("Felaktig Månad");
        }

        if (Manad == 1 || Manad == 3 || Manad == 5 || Manad == 7 || Manad == 8 || Manad == 10 || Manad == 12) {
            System.out.print("Ange Dag>");
            Dag = scanner1.nextInt();

            // BAD IF
            if (Dag > 0 && Dag < 32);

            System.out.print("Korrekt Datum");
        } else {
            System.out.print("Felaktig Dag i Månaden");
        }

        if (Manad == 2) {
            System.out.print("Ange Dag");
            Dag = scanner1.nextInt();

            // BAD IF
            if (Dag > 0 && Dag < 29);

            System.out.print("Korrekt Datum");
        } else {
            System.out.print("Felaktig Dag i Månaden");
        }

        if (Manad == 4 || Manad == 6 || Manad == 9 || Manad == 11) {
            System.out.println("Ange Dag");
            Dag = scanner1.nextInt();

            // BAD IF
            if (Dag > 0 && Dag < 31);

            System.out.print("Korrekt Datum");
        } else {
            System.out.print("Felaktig Dag i Månaden");

            scanner1.close();

        }
    }
}

You have issues with your braces, see this code: if您的大括号有问题,请参阅此代码:如果

(Manad == 1 || Manad == 3 || Manad == 5 || Manad == 7 || Manad == 8 || Manad == 10 || Manad == 12) {
        System.out.print("Ange Dag>");
        Dag = scanner1.nextInt();
        if (Dag > 0 && Dag < 32); 
            System.out.print("Korrekt Datum");
            }
            else {
                System.out.print("Felaktig Dag i Månaden");
        }

Your first closing brace } actually closes the main if.你的第一个大括号}实际上关闭了主要的 if。 The code could be seen as:代码可以看成:

if (Manad == 1 || Manad == 3 || Manad == 5 || Manad == 7 || Manad == 8 || Manad == 10 || Manad == 12) {
    System.out.print("Ange Dag>");
    Dag = scanner1.nextInt();
    if (Dag > 0 && Dag < 32); 
        System.out.print("Korrekt Datum");   
}
else {
    System.out.print("Felaktig Dag i Månaden");
}

在这一行中用括号替换分号:

if (Dag > 0 && Dag < 32);

You're Gavin said, the execution doesn't stop just because you enter a else statement, it keeps going from that point.你是 Gavin 说的,执行不会因为你输入 else 语句而停止,它会从那一刻开始。 If you want it to work as you expect it to you either have to put the code that should run only if the if-statement is true inside the if-block or introduce a return statement.如果您希望它按预期工作,则必须将仅当 if 语句为真时才应运行的代码放入 if 块中,或者引入 return 语句。

if(foo == 1) {
  if(bar == 2 {
    //Do something
  }
  else {
    //bar is not 2
  }
}
else {
  //foo is not 1
}

or或者

if(foo == 1) {
}
else {
  //foo is not 1
  return;
}
if(bar == 2 {
  //Do something
}
else {
  //bar is not 2
  return;
}

You need to put your if(Manad == 2) inside the the else statement that belongs to if(Manad == 1..... Also put the if (Manad == 4.... inside the else statement of (Manad ==2). I did some comments and removed some brackets.您需要将 if(Manad == 2) 放在属于 if(Manad == 1..... Manad ==2). 我做了一些评论并删除了一些括号。

    Scanner scanner1 = new Scanner(System.in);

int Manad, Dag;
System.out.print("Ange Månad>");

Manad = scanner1.nextInt();
if (Manad > 0 && Manad < 13) {

    // you're not doing something here
}
else { 
    System.out.print("Felaktig Månad");
}

if (Manad == 1 || Manad == 3 || Manad == 5 || Manad == 7 || Manad == 8 || Manad == 10 || Manad == 12) {
    System.out.print("Ange Dag>");
    Dag = scanner1.nextInt();
    if (Dag > 0 && Dag < 32) {
        System.out.print("Korrekt Datum");
    }
}
else { // called if Manad != 1,3,5,7,8,10, or 12
    System.out.print("Felaktig Dag i Månaden");
}

if (Manad == 2) { // you may wanna put this if else statement inside the previous else
    System.out.print("Ange Dag");
    Dag = scanner1.nextInt();
    if (Dag > 0 && Dag < 29) {
        System.out.print("Korrekt Datum");
    }
}
else { // for now, this is called every time Mannad != 2 
    System.out.print("Felaktig Dag i Månaden");
}

if (Manad == 4 || Manad == 6 || Manad == 9 || Manad == 11) { 
    // put this if else statement inside else that belongs to Manad == 2
    System.out.println("Ange Dag");
    Dag = scanner1.nextInt();
    if (Dag > 0 && Dag < 31) {
        System.out.print("Korrekt Datum");
    }
}
else {// for now, this is called every time Manad != 4,6,9, or 11
    System.out.print("Felaktig Dag i Månaden");

    scanner1.close();

}

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

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