vendredi 20 septembre 2019

Why if the input is String the program adds point

I made easy game, the goal of game is to collect more points than computer. I have problem with if statements. If user input is String it add points to user or computer. Do you know why? Here is the code:

 private static void mainGame() {
    System.out.println(options);
    System.out.print("=> ");
    try {
        userChoice = Integer.parseInt(reader.readLine());
    } catch (NumberFormatException | IOException e) {
        System.out.println(RED + pickNumber + RESET);
    }
    computerChoice = random.nextInt(3) + 1;
    if (userChoice == 1 && computerChoice == 2) {
        computerPoints++;
        System.out.println(YELLOW_BOLD + you + YELLOW + papier + BLUE_BOLD + computer + BLUE + nozyce + "\n" +
                BLUE_BOLD + pointForComputer + YELLOW_BOLD + userPoints + ":" + BLUE_BOLD + computerPoints + RESET);
    } else if (userChoice == 1 && computerChoice == 3) {
        userPoints++;
        System.out.println(YELLOW_BOLD + you + YELLOW + papier + BLUE_BOLD + computer + BLUE + kamien + "\n" +
                YELLOW_BOLD + pointForUser + YELLOW_BOLD + userPoints + ":" + BLUE_BOLD + computerPoints + RESET);
    } else if (userChoice == 2 && computerChoice == 1) {
        userPoints++;
        System.out.println(Finals.YELLOW_BOLD + you + YELLOW + nozyce + BLUE_BOLD + computer + BLUE + papier + "\n" +
                YELLOW_BOLD + pointForUser + YELLOW_BOLD + userPoints + ":" + BLUE_BOLD + computerPoints + RESET);
    } else if (userChoice == 2 && computerChoice == 3) {
        computerPoints++;
        System.out.println(YELLOW_BOLD + you + YELLOW + nozyce + BLUE_BOLD + computer + BLUE + kamien + "\n" +
                BLUE_BOLD + pointForComputer + YELLOW_BOLD + userPoints + ":" + BLUE_BOLD + computerPoints + RESET);
    } else if (userChoice == 3 && computerChoice == 1) {
        computerPoints++;
        System.out.println(YELLOW_BOLD + you + YELLOW + kamien + BLUE_BOLD + computer + BLUE + papier + "\n" +
                BLUE_BOLD + pointForComputer + YELLOW_BOLD + userPoints + ":" + BLUE_BOLD + computerPoints + RESET);
    } else if (userChoice == 3 && computerChoice == 2) {
        userPoints++;
        System.out.println(YELLOW_BOLD + you + YELLOW + kamien + BLUE_BOLD + computer + BLUE + nozyce + "\n" +
                YELLOW_BOLD + pointForUser + YELLOW_BOLD + userPoints + ":" + BLUE_BOLD + computerPoints + RESET);
    } else if (userChoice == computerChoice) {
        System.out.println(WHITE_BOLD + samePickAgain + RESET);
    } else if (userChoice>3){
        System.out.println(RED + wrongChoice + RESET);
    }
}

Aucun commentaire:

Enregistrer un commentaire