lundi 19 août 2019

if statement within while loop does not give the right answer

i am making a guessing game using arrays in java. this project will be the core of my learning by doing project where i want to build a battleship game. to check my guessed position i gave it 2 while loops. one for row guessing and the other for column guessing. within the while loop i included an if statement to make a decision if the guessed value is valid. the problem is that the program is accepting the zero as a guessed value in spite of i put it as non-valid within the if statement using or (||) operator. how can i get it to work properly?

i was trying to make a separate class for testing the guesses process but it didn't work will so i included the code in the main method.

    System.out.print("row: ");
    int row_guessing=input.nextInt();
            boolean accepted_r=false;
    while (accepted_r==false) {
        if ((row_guessing)>row || (row_guessing)==0)  {
            System.out.println("you should give an integer number between 1 and "+(row));
            System.out.println("give a new value for please:");
            row_guessing=input.nextInt();
        }
        else {
            accepted_r=true;
        }
    System.out.print("column: ");
    int column_guessing=input.nextInt();
        boolean accepted_c=false;
    while (accepted_c==false) {
        if ((column_guessing)>column || (column_guessing)==0) {
            System.out.println("you should give an integer number between 1 and "+(column));
            System.out.println("give a new value for please:");
            column_guessing=input.nextInt();
        }
        else {
                accepted_c=true;
        }

    //guessing.test(column_guessing, column-1);
    System.out.println("you have chosen the position ["+row_guessing+","+column_guessing+"]");
    Check_position.check(array1, row_guessing-1, column_guessing-1);
    }
}
}
```


i expected to refuse the 0 as a guess but it didn't

Aucun commentaire:

Enregistrer un commentaire