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