переменная из оператора if не считается правильно - PullRequest
0 голосов
/ 13 июня 2018

Я сделал программу викторины в Java.Все работает, но счетчик в конце, который подсчитывает количество вопросов, на которые были даны правильные ответы, неправильно подсчитывается и всегда говорит «3 из 3», даже если на все вопросы даны неправильные ответы.Что я сделал не так?

import java.util.Scanner;

class quiz {
public static void main(String args[]) {
    Scanner input = new Scanner(System.in);

    int q1, q2, q3, result;
    boolean onepoint, twopoint, threepoint;

    result = 0;

    onepoint = false;
    twopoint = false;
    threepoint = false;

    System.out.println("Interactive quiz");
    System.out.println("");

    System.out.println("Q1) Which country is new york located in?");
    System.out.println("    1) Canada");
    System.out.println("    2) United States");
    System.out.println("    3) China");
    System.out.println("    4) Russia");

    System.out.println("");
    System.out.print("> ");
    q1 = input.nextInt();

    if (q1 == 2) {
        System.out.println("Your answer is correct.");
        onepoint = true;
    }
    else if (q1 != 2) {
        System.out.println("Your answer is incorrect.");
        threepoint = false;
    }

    System.out.println("");

    System.out.println("Q2) Which of these animals are not warm blooded?");
    System.out.println("    1) Bear");
    System.out.println("    2) Crow");
    System.out.println("    3) Elephant");
    System.out.println("    4) Frog");

    System.out.println("");
    System.out.print("> ");
    q2 = input.nextInt();

    if (q2 == 4) {
        System.out.println("Your answer is correct.");
        twopoint = true;
    }
    else if (q2 != 4) {
        System.out.println("Your answer is incorrect.");
        threepoint = false;
    }

    System.out.println("");

    System.out.println("Q3) Which of these plants are carnivorous?");
    System.out.println("    1) Dandelion");
    System.out.println("    2) rafflesia");
    System.out.println("    3) cape sundew");
    System.out.println("    4) titan arum");

    System.out.println("");
    System.out.print("> ");
    q3 = input.nextInt();

    if (q3 == 3) {
        System.out.println("Your answer is correct.");
        threepoint = true;
    }
    else if (q3 != 3) {
        System.out.println("Your answer is incorrect.");
        threepoint = false;
    }

    System.out.println("");

    if (onepoint = true) {
        result = result + 1;
    }
    if (twopoint = true) {
        result = result + 1;
    }
    if (threepoint = true) {
        result = result + 1;
    }

    System.out.println("Your final score is " + result + " out of 3.");
    }
}

У меня такое ощущение, что это, вероятно, связано с моими утверждениями if в конце.Я пытался поиграть с ними, но все равно не мог заставить его работать.

1 Ответ

0 голосов
/ 13 июня 2018

Это все задания, point = true.Используйте оператор равенства или логическое значение в качестве условия

if (onepoint == true) {
    result = result + 1;
}

или

if (onepoint) {
    result = result + 1;
}

Если вы используете логические значения в качестве трекеров точек, вы можете рассмотреть возможность добавления значений точек сразу после определения ответа,например:

public static void main(String args[]) {
    Scanner input = new Scanner(System.in);
    int result = 0;

    System.out.println("Interactive quiz\n");
    System.out.println("Q1) Which country is new york located in?");
    System.out.println("    1) Canada");
    System.out.println("    2) United States");
    System.out.println("    3) China");
    System.out.println("    4) Russia");
    System.out.println("");
    System.out.print("> ");

    if(input.nextInt() == 2){
        System.out.println("Your answer is correct.");
        result++
    }else
        System.out.println("Your answer is incorrect.");

    System.out.println("");
    System.out.println("Q2) Which of these animals are not warm blooded?");
    System.out.println("    1) Bear");
    System.out.println("    2) Crow");
    System.out.println("    3) Elephant");
    System.out.println("    4) Frog");
    System.out.println("");
    System.out.print("> ");

    if (input.nextInt() == 4) {
        System.out.println("Your answer is correct.");
        result++;
    }else
        System.out.println("Your answer is incorrect.");

    System.out.println("");
    System.out.println("Q3) Which of these plants are carnivorous?");
    System.out.println("    1) Dandelion");
    System.out.println("    2) rafflesia");
    System.out.println("    3) cape sundew");
    System.out.println("    4) titan arum");
    System.out.println("");
    System.out.print("> ");

    if (input.nextInt() == 3) {
        System.out.println("Your answer is correct.");
        result++;
    }else
        System.out.println("Your answer is incorrect.");

    System.out.println("");
    System.out.println("Your final score is " + result + " out of 3.");
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...