布尔值总是 returns 我在初始化时分配的任何值,但它不检查条件语句

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

我花了两个小时试图找出为什么布尔表达式总是 returns 从一开始就赋值,而不是从正确的条件语句中取值。 在这种情况下,即使我的输入是 ACDquestionOneAnswer() 方法 returns 中的 boolean question 仍然为真,这意味着即使答案错误也会加分。 但是,如果我在 questionOneAnswer() 中将 boolean question 分配给 false 并且我的输入是 B (这是正确的答案) 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() current,old 和 new 都设置为 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 认为它正在做的任何事情似乎已经解决了问题。