2014-10-01 118 views
-2

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

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

你应该编辑你以前的问题,使其更清楚,并包括这个额外的代码。 – mdewitt 2014-10-01 00:56:16

+1

可能重复的[字符串输入问题?](http://stackoverflow.com/questions/26131721/string-input-problems) – mdewitt 2014-10-01 00:56:25

+0

尽管我很愉快***赢***斯科辛,你已经(记住,威斯康星州是“罪恶牛罪恶”的一个字谜),而且你有獾和狼獾。另外,你应该考虑修正原来的*不清楚的问题,而不是问一个新的问题,特别是如果它最终是同一个问题。 – 2014-10-01 01:00:37

回答

0

我觉得你的代码不正确地拍摄用户的答案,它只是提示的问题,并立即再次无法打印PROMPT1给出了答案的机会。这就是为什么它不符合你的答案,只是在你的分数下降的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--; 
        } 

       }