dimanche 17 décembre 2017

Changing Integer Value inside an if and else statement

I'm trying to create some Java code in an if-and-else statement that goes between the two. When I run the code, the expected output should be: "hello world hello world" but all I get is "hello hello hello hello"

I have no clue what I'm doing wrong over here. Can somebody please tell me the problem?

    int p = 1;

    for(int i = 1; i < 5; i++){
    if (p == 1){    
        System.out.println("hello");
        p = 2;
    }
    else 
        System.out.println("world");
        p = 1;
    }

Aucun commentaire:

Enregistrer un commentaire