-2

I asked this question before, but I think that because i only included as piece of the code it was unclear. I am writing a program that asks the user what kind of question they want asked, and then they are prompted for an answer to that question. However, even the correct input from the user results in a return of an incorrect answer. Here's the code:

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);
    }
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 1
    You should edit your previous question to make it more clear and include this extra code. – mdewitt Oct 01 '14 at 00:56
  • 1
    possible duplicate of [String input problems?](http://stackoverflow.com/questions/26131721/string-input-problems) – mdewitt Oct 01 '14 at 00:56
  • As much as I'm amused by ***Win*** sconsin, you've got a typo there (remember, Wisconsin is an anagram of "sin cow sin") and you've got Badgers and Wolverines backwards. Also, you should look at fixing the original *unclear* question rather than asking a new one, especially if its ultimately the same question. –  Oct 01 '14 at 01:00
  • but i suggest to not to use do while loop for the same. – Kishan Bheemajiyani Oct 01 '14 at 05:26

1 Answers1

0

I think your code is not taking the user answers correctly,it is just prompting the question and immediately print your prompt1 again and not giving the chance for answer. That's why it is not matching your answer and just going in the else loop where your score get decrease. Another thing in your each if/ else statement you are using scan.next() that's why it is just reading only one word not a line.

I just make few changes in your code and it is running. I made few change in your if else condition do the same with others. I hope it will work for you -

        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--;
                    }

                } 
Moni
  • 433
  • 3
  • 9