簡體   English   中英

布爾值始終返回初始化時分配的任何值,但不檢查條件語句

[英]Boolean always returns whatever value I assign when initialised, but it doesn't check the conditional statement

我花了兩個小時試圖找出為什么布爾表達式始終返回從頭開始分配的任何值,而不是從正確的條件語句中獲取值。 在這種情況下,即使我的輸入是ACD ,來自questionOneAnswer()方法的boolean question也會返回true,這意味着即使答案是錯誤的,也要加上分數。 但是,如果我在questionOneAnswer()中將boolean question分配為false,並且我的輸入為B (這是正確的答案), qOneCheck將不執行score()方法qOneCheck的代碼,因此,分數保持為0。

      import java.util.Scanner;     
      import java.util.Random;
      class miniProject
      {
         public static void main(String[] args)
         {

             questionOne(); 
             questionOneAnswer();
             int scr = score();

             System.out.println("Your score is " + scr);



             System.exit(0); 
          }


            /* *********************************
            This method asks for a question and gives 4 possible answers
            */ 
            public static void questionOne()
            {
               System.out.println("Please, type the letter which you think 
               contain the right answer!");
               System.out.println("  ");
               System.out.println("How many oscars did the Titanic movie 
               got?");
               System.out.println("A. 12    B.11    C.3    D.32");

               return; // Ends the method 

            }
           public static int score()
           {    
              boolean qOneCheck = questionOneAnswer();
              int currentScore = 0;
              int oldScore = 0;
              int newScore = 0;
              int random = randomGen();


             if(qOneCheck == true)
          {
             currentScore = currentScore + random;
             newScore = currentScore;

          }
          else 
          {
            currentScore = oldScore;
          }     



            return newScore;
          }   



        public static  boolean questionOneAnswer()
        {
             boolean question = true;
             String i = input();


             if (i.equalsIgnoreCase("A"))
             {
                 System.out.println("False, you don't get any points!");
                 question = false;
             }

             else if (i.equalsIgnoreCase("B"))
             {
               System.out.println("You answer is correct");

              question = true;
             }

             if (i.equalsIgnoreCase("C"))
             {
              System.out.println("False, you don't get any points!");
                  question = false;
             }

            if (i.equalsIgnoreCase("d"))
            {
                  System.out.println("False, you don't get any points!");
                question = false;
            }



            return question;//Ends method 
         }

          /* *********************************
          This method receives input from user and stors it in String 
             called answer
          */ 
          public static String input()
          {      
               Scanner scanner = new Scanner(System.in); 
               String answer;
               answer = scanner.nextLine();

              return answer; // returns String answer when method is 
             called 

            }



         public static int randomGen()
         {
              Random score = new Random();

              int score1 = score.nextInt(10) +1;



               return score1;   


           }


        }

編輯:刪除questioOneAnswer()我終於得到了結果。 感謝你們大家。 我終於要去睡覺了,現在哈。

好的,除了其他方面,您的代碼還存在以下問題:

在此處輸入圖片說明

我注釋掉了我們還沒有的部分,並且有效:

public static void main(String[] args) {
    int scr = score();
//      questionOne();
//      questionOneAnswer();
    System.out.println("Your score is " + scr);
    System.exit(0);
}

我不知道為什么你要打電話給questionOneAnswer(); 第二次(一次在score內-將值分配給臨時變量),再次在main里甚至不分配返回值的布爾值。

public static int score() {
    boolean qOneCheck = questionOneAnswer();
    int currentScore = 0;
    int oldScore = 0;
    int newScore = 0;
//      int random = randomGen();
    if (qOneCheck == true) {
//          currentScore = currentScore + random;
        currentScore = currentScore + 1;
        newScore = currentScore;
    } else {
        currentScore = oldScore;
        newScore = currentScore;
    }
    return newScore;
}

其中大部分是垃圾。 每次您調用score()時,當前,新舊都設置為0。

就像是:

static int runningTotal = 0;

public static int score() {
    if (questionOneAnswer()) runningTotal++;
}

或用一塊石頭殺死兩只鳥:

public static void main(String[] args) {
    score();
    System.out.println("Your score is " + scr);
    System.exit(0);
}

static int scr = 0;

public static int score() {
    if (questionOneAnswer()) scr++;
}

也是默認的questionOneAnswer(); 不應該為真,應該為假,然后您可以解決我導致的問題(虛假輸入將為假)。 但是,不僅如此,我們還可以簡化該方法:

public static boolean questionOneAnswer() {
    String i = input();
    return i.equalsIgnoreCase("B");
}

我沒有看輸入法。

TLDR:注釋掉main方法中的兩條多余的行,而randomgen認為它所做的一切似乎都已解決了該問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM