简体   繁体   中英

if and else statements not working as intended

I am making a really simple program, mostly to learn the if/else statements. 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.
  • 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

MonthWrongWrongdayinthemonthWrongdayinthemonth

instead of just

month wrong

Is this because I have if/else statements inside of other if/else statements? 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. For example, you write:

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

This is equivalent to having no if statement at all!

An if statement has the following form:

if (condition)
    statement

and an if-else statement has the following form:

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. So when you end your if statement with a semicolon, you're actually writing

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. 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). 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.

Below is your code formatted better so you can see the mistakes you've made. There are 3 if statements that end in ; , 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. This is what you are seeing. To fix this, remove the ; at the end of the if statements, and add { . (I marked the bad ifs with // BAD IF )

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. 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. 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(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.

    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();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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