jeudi 1 novembre 2018

Java If statement(s) seemingly being ignored [on hold]

I have multiple instances in my code in which an if condition is being ignored. Here's my code (starting at line 62):

System.out.println("Human, will you attack? Yes: 1 No: 2");
            answer = (int) System.in.read();
            if(answer == 50);{
            System.out.println("Human chose not to attack");
            }
            if(answer == 49);{
            System.out.println("Human chose to attack");
            Attack = (int)(Math.random()*10) +1;
            System.out.print("Human's Attack: ");
                for(int i = 1; i <= Attack; i++){
                    System.out.print(">");
                }
                System.out.print("(" + Attack + ")\n");
            Thread.sleep(1000);
            if(Defense < Attack);{
            pcLife = pcLife -1;
            System.out.println("PC lost 1 life");
            }
            if(Defense > Attack);{
            humanLife = humanLife -1;
            System.out.println("Human lost 1 life");
            }
            }

Both of the if conditions after the prompt at line 1 ("Human, will you attack") execute, even though the variable "answer" cannot possibly be two numbers at the same time.

The same problem occurs later, where I compare the "defense" and "attack" variables to each other. Yet again, both execute.

I tested it manually, and defense == attack always returns true, as does if(answer == 49) after System.in.read().

Why does this happen, and what can I do to fix it so that the conditions function properly?

Another question that'd be nice to know the answer to is how I can change the int "answer" after the System.in.read() prompt to appear as a 1 and not the ASCII code for the number 1.

Whole code:

public static void main(String[] args) throws IOException, InterruptedException {
    int humanLife = 10;
    int pcLife = 10;
    int Defense;
    int Attack;
    int playerTurn;
    int answer;

    System.out.println("Choose who starts (Human: 1 PC: 2)");
    playerTurn = (int) System.in.read();

    while(humanLife > 0 && pcLife > 0){
        if(playerTurn == 49){
            System.out.println("-----------------------\nHuman's turn");
            Defense = (int)(Math.random()*10) +1;
            System.out.print("Human's Defense: ");
                for(int i = 1; i <= Defense; i++){
                    System.out.print("#");
                }
                System.out.print("(" + Defense + ")\n");
            Thread.sleep(1000);
            System.out.println("PC attacks");
            Attack = (int)(Math.random()*10) +1;
            System.out.print("PC's Attack: ");
                for(int i = 1; i <= Attack; i++){
                    System.out.print(">");
                }
                System.out.print("(" + Attack + ")\n");
            Thread.sleep(1000);
            if(Defense < Attack);{
            humanLife = humanLife -1;
            System.out.println("Human lost 1 life");
            }
            if(Defense > Attack);{
            pcLife = pcLife -1;
            System.out.println("PC lost 1 life");
            }
        System.out.println("Human's Life: " + humanLife);
        System.out.println("PC's Life: " + pcLife);
        playerTurn = 50;
        }

        if(playerTurn == 50){
            System.out.println("-----------------------\nPC's turn");
            Defense = (int)(Math.random()*10) +1;
            System.out.print("PC's Defense: ");
                for(int i = 1; i <= Defense; i++){
                    System.out.print("#");
                }
                System.out.print("(" + Defense + ")\n");
            Thread.sleep(1000);
            System.out.println("Human, will you attack? Yes: 1 No: 2");
            answer = (int) System.in.read();
            if(answer == 50);{
            System.out.println("Human chose not to attack");
            }
            if(answer == 49);{
            System.out.println("Human chose to attack");
            Attack = (int)(Math.random()*10) +1;
            System.out.print("Human's Attack: ");
                for(int i = 1; i <= Attack; i++){
                    System.out.print(">");
                }
                System.out.print("(" + Attack + ")\n");
            Thread.sleep(1000);
            if(Defense < Attack);{
            pcLife = pcLife -1;
            System.out.println("PC lost 1 life");
            }
            if(Defense > Attack);{
            humanLife = humanLife -1;
            System.out.println("Human lost 1 life");
            }
            }
        System.out.println("Human's Life: " + humanLife);
        System.out.println("PC's Life: " + pcLife);
        playerTurn = 49;
    }
}
    if(humanLife < 1){
        System.out.println("◄PC wins!► \nRemaining Life:" + pcLife);
    }else{
        System.out.println("◄Human wins!► \nRemaining Life:" + humanLife);
    }
}

Aucun commentaire:

Enregistrer un commentaire