来自if语句的变量没有正确计数

时间:2018-06-13 14:30:17

标签: java boolean

我在java中制作了一个测验程序。一切正常,但最终的计数器计算了正确回答的问题数量并没有正确计算,并且即使所有问题都回答错误,也总是说“三分之三”。我做错了什么?

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 :(得分:1)

这些都是作业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.");
}