dimanche 28 avril 2019

My if-else program keeps printing both, if and else, why?

I tried to program a simple if-else code, its supposed to be a interview sort of thing which asks you questions and you put in the answer.

When I run the .exe and I put in the conditions of the if command, it prints both the if and the else statement, even though the requirements of else haven't been met. Why is that?

already tried to use only if in both statements. tried changing the syntax etc.

import java.io.IOException;
import java.util.Scanner;

public class Abfrage {

 public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner scanner = new Scanner(System.in);
    System.out.println("Wie ist dein Geschlecht? M oder W?");
    String geschlecht = scanner.nextLine();

    System.out.println("Wie ist dein Alter?");
    int alter = Integer.parseInt(scanner.nextLine());
    if (alter >= 18 && alter <= 100) {
        System.out.println("Du bist volljaehrig, herzlichen Glueckwunsch!");
    } 
    else if(alter >= 0 && alter <= 17);
        System.out.println("Du bist leider noch minderjaehrig mein Kind.");
    }

 }

I want the program to give out ("Du bist volljaehrig, herzlichen Glueckwunsch!") when I put in a number between 18 and 100. When I put in a number between 0 and 18 its supposed to tell me "Du bist leider noch minderjaehrig mein Kind."

When I put in 16 for example, it only gives me the else if output which is as I expected. When I put in 19 for example, it gives me both outputs.

Aucun commentaire:

Enregistrer un commentaire