错误的测验结果

时间:2015-10-27 15:18:57

标签: java if-statement

This是程序的输出

我可以通过以下方式获得this result

import java.util.Scanner;

public class aLittleQuiz {

    public static void main(String[] args) {

        // declaring varibles

        String quizStart;
        int quizAns1, quizAns2, quizAns3;

        Scanner input = new Scanner(System.in);

        System.out.println("Are you ready for a quiz? (y / n)");
        quizStart = input.next();

        System.out.println("Okay, here it comes!");

        // quiz answer 1

        System.out.println("\nWhat is the capital of Alaska? \n1) Melbourne\n2) Anchorage\n3) Juneau");
        quizAns1 = input.nextInt();

        if (quizAns1 == 3) {
            System.out.println("That's right");
        } else {
            System.out.println("Your answer is wrong, sorry!");
        }

        // quiz answer 2

        System.out.println("Q2) Can you store the value ''cat'' in a variable of type int? \n1) yes \n2) no");
        quizAns2 = input.nextInt();

        if (quizAns2 == 1) {
            System.out.println("Sorry, ''cat'' is a string. ints can only store numbers.");
        } else if (quizAns2 == 2) {
            System.out.println("Correct!");
        }

        // quiz answer 3

        System.out.println("What is the result of 9+6/3? \n1) 5\n2) 11 \n3) 15/3");
        quizAns3 = input.nextInt();
        if (quizAns3 == 2) {
            System.out.println("That's correct!");
        } else {
            System.out.println("");
        }

        // if (quizAns == 3 && quizAns == ) {

        // }

    }

}

但我如何编程这部分?

  

"总的来说,你有3个中有2个是正确的。   感谢您的参与!"

1 个答案:

答案 0 :(得分:4)

声明一个类似int marks的变量,并在if\else块内增加一个(这样可以得到正确的答案)。并在最后打印

System.out.println("Overall, you got" +marks+" out of 3 correct. Thanks for playing!");

假设你的问题没有解决(3)

        String quizStart;
        int quizAns1, quizAns2, quizAns3;
        int marks=0;

        Scanner input = new Scanner(System.in);

        System.out.println("Are you ready for a quiz? (y / n)");
        quizStart = input.next();

        System.out.println("Okay, here it comes!");

        // quiz answer 1

        System.out.println("\nWhat is the capital of Alaska? \n1) Melbourne\n2) Anchorage\n3) Juneau");
        quizAns1 = input.nextInt();

        if (quizAns1 == 3) {
            System.out.println("That's right");
            ++marks;
        } else {
            System.out.println("Your answer is wrong, sorry!");
        }

        // quiz answer 2

        System.out.println("Q2) Can you store the value ''cat'' in a variable of type int? \n1) yes \n2) no");
        quizAns2 = input.nextInt();

        if (quizAns2 == 1) {
            System.out.println("Sorry, ''cat'' is a string. ints can only store numbers.");
        } else if (quizAns2 == 2) {
            System.out.println("Correct!");
            ++marks;
        }

        // quiz answer 3

        System.out.println("What is the result of 9+6/3? \n1) 5\n2) 11 \n3) 15/3");
        quizAns3 = input.nextInt();
        if (quizAns3 == 2) {
            System.out.println("That's correct!");
            ++marks;
        } else {
            System.out.println("");
        }
        System.out.println("Overall, you got " +marks+" out of 3 correct. Thanks for playing!");
相关问题