多个单词串输入和布尔值

时间:2014-10-01 00:50:01

标签: java

之前我问过这个问题,但我认为因为我只是作为代码片段包含在内,所以不清楚。我正在编写一个程序,询问用户他们想要问什么样的问题,然后提示他们回答这个问题。但是,即使来自用户的正确输入也会导致返回错误答案。这是代码:

import java.util.Scanner;


public class MascotQuiz {

    public static void main(String[] args) {

        int score = 0;

        String greeting = "In this game, I ask you four questions about mascots for "
                + "US collegiate sports teams."
                + "\nYou get 1 point for each correct answer, "
                + "0 points if you type don't know, "
                + "and you lose a point for wrong answers.";
        final String schoolOptions = "University of Michigan, "
                + "University of Nebraska, " + "University of Oklahoma, "
                + "University of Wisconsin";
        final String mascotOptions = "Badgers, Cornhuskers, Sooners, Wolverines";
        String prompt1 = "\nType 1 and I'll give you the mascot and "
                + "you give give the school. \n"
                + "Type 2 and I'll give you the school and "
                + "you give me the mascot. \n" + "Type 3 and I'll quit.";

        System.out.println(greeting);



        int questionCount = 1;

        String mascotQuestion1, mascotQuestion2, mascotQuestion3, mascotQuestion4;
        String schoolQuestion1, schoolQuestion2, schoolQuestion3, schoolQuestion4;

        do {

            System.out.println(prompt1);
            int optionChoice;
            Scanner scan = new Scanner(System.in);
            optionChoice = scan.nextInt();

            if (optionChoice == 1) {

                if (questionCount == 1) {
                    System.out.println("What school do the Badgers belong to?");
                    mascotQuestion1 = scan.nextLine();
                    if (mascotQuestion1.equalsIgnoreCase("University of Michigan")) {
                        score++;

                    } 
                    else if (mascotQuestion1.equalsIgnoreCase("don't know")) {
                        score = (score + 0);
                    } 
                    else {
                        score--;
                    }

                } 
                else if (questionCount == 2) {
                    System.out.println("What school do the Cornhuskers belong to?");
                    mascotQuestion2 = scan.next();
                    if (mascotQuestion2.equalsIgnoreCase("University of Nebrasksa")) {
                        score++;
                    }
                    else if (mascotQuestion2.equalsIgnoreCase("don't know")) {
                        score = (score + 0);
                    } 
                    else {
                        score--;
                    }

                } 
                else if (questionCount == 3) {
                    System.out.println("What school do the Sooners belong to?");
                    mascotQuestion3 = scan.next();
                    if (mascotQuestion3.equalsIgnoreCase("University of Oklahoma")) {
                        score++;
                    } 
                    else if (mascotQuestion3.equalsIgnoreCase("don't know")) {
                        score = (score + 0);
                    } 
                    else {
                        score--;
                    }

                } 
                else {
                    System.out.println("What school do the Wolverines belong to?");
                    mascotQuestion4 = scan.next();
                    if (mascotQuestion4.equalsIgnoreCase("University of Winsconsin")) {
                        score++;
                    }
                    else if (mascotQuestion4.equalsIgnoreCase("don't know")) {
                        score = (score + 0);
                    } 
                    else {
                        score--;
                    }

                }

            }
            else if (optionChoice == 2) {

                if (questionCount == 1) {
                    System.out.println("What mascot belongs to the University of Michigan?");
                    schoolQuestion1 = scan.next();
                    if (schoolQuestion1.equalsIgnoreCase("Badgers")){
                        score++;
                    }
                    else if (schoolQuestion1.equalsIgnoreCase("don't know")){
                        score = score + 0;
                    }
                    else {
                        score --;
                    }

                }
                else if (questionCount == 2) {
                    System.out.println("What mascot belongs to the University of Nebraska?");
                    schoolQuestion2 = scan.next();
                    if (schoolQuestion2.equalsIgnoreCase("Cornhuskers")){
                        score++;
                    }
                    else if (schoolQuestion2.equalsIgnoreCase("don't know")){
                        score = score + 0;
                    }
                    else {
                        score --;
                    }

                }
                else if (questionCount == 3) {
                    System.out.println("What mascot belongs to the University of Oklahoma?");
                    schoolQuestion3 = scan.next();
                    if (schoolQuestion3.equalsIgnoreCase("Sooners")){
                        score++;
                    }
                    else if (schoolQuestion3.equalsIgnoreCase("don't know")){
                        score = score + 0;
                    }
                    else {
                        score --;
                    }

                } 

                else {
                    System.out.println("What mascot belongs to the University of Wisconsin?");
                    schoolQuestion4 = scan.next();
                    if (schoolQuestion4.equalsIgnoreCase("Wolverines")){
                        score++;
                    }
                    else if (schoolQuestion4.equalsIgnoreCase("don't know")){
                        score = score + 0;
                    }
                    else {
                        score --;
                    }

                }

            }

            else {
                questionCount = 5;
            }
            questionCount ++;

        } while (questionCount <= 4);

System.out.println("\nBye. Your score is " + score);
    }
}

1 个答案:

答案 0 :(得分:0)

我认为您的代码没有正确地回答用户的问题,它只是提示问题并立即再次打印您的提示1并且没有给出回答的机会。这就是为什么它不匹配你的答案,只是进入你的分数减少的else循环。 你在每个if / else语句中使用scan.next()的另一件事就是为什么它只是只读一个单词而不是一行。

我只是对您的代码进行了一些更改,它正在运行。我做了一些改变你的if else条件和其他人做同样的事情。我希望它对你有用 -

        Scanner scan ; // declare scanner reference here and use later 
        do {

            System.out.println(prompt1);
            int optionChoice;
            scan = new Scanner(System.in); // create new scanner object
            optionChoice = scan.nextInt();

            if (optionChoice == 1) {

                if (questionCount == 1) {
                    System.out.println("What school do the Badgers belong to?");
                    scan = new Scanner(System.in); // create new scanner object, it will allow user to enter answer

                    mascotQuestion1 = scan.nextLine(); // use nextline it will read full line otherwise it is just reading "university" not full answer

                    if (mascotQuestion1.equalsIgnoreCase("University of Michigan")) {
                        score++;
                    } 
                    else if (mascotQuestion1.equalsIgnoreCase("don't know")) {
                        score = (score + 0);
                    } 
                    else {
                        score--;
                    }

                } 
                else if (questionCount == 2) {
                    System.out.println("What school do the Cornhuskers belong to?");
                    scan = new Scanner(System.in); // create new scanner object, it will allow user to enter answer

                    mascotQuestion2 = scan.nextLine();// use nextline it will read full line otherwise it is just reading "university" not full answer

                    if (mascotQuestion2.equalsIgnoreCase("University of Nebrasksa")) {
                        score++;
                    }
                    else if (mascotQuestion2.equalsIgnoreCase("don't know")) {
                        score = (score + 0);
                        System.out.println("score 2****----"+score);
                    } 
                    else {
                        score--;
                    }

                }