简体   繁体   English

布尔值始终返回初始化时分配的任何值,但不检查条件语句

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

I spent two hours trying to find out why the boolean expression always returns whatever value is assigned from the beginning instead of taking the value from the correct conditional statement. 我花了两个小时试图找出为什么布尔表达式始终返回从头开始分配的任何值,而不是从正确的条件语句中获取值。 In this case, the boolean question from the questionOneAnswer() method returns true even if my input is A or C or D , which means the score is added even if the answer is wrong. 在这种情况下,即使我的输入是ACD ,来自questionOneAnswer()方法的boolean question也会返回true,这意味着即使答案是错误的,也要加上分数。 However, if I assign boolean question to false in questionOneAnswer() and my input is B (which is the correct answer) the code in score() method qOneCheck is not executed, therefore, score stays at 0. 但是,如果我在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;   


           }


        }

EDIT: After removing questioOneAnswer() I finally got the result. 编辑:删除questioOneAnswer()我终于得到了结果。 Thanks to all of you guys. 感谢你们大家。 I finally gona go to sleep now hah. 我终于要去睡觉了,现在哈。

Okay, well among other things your code has this problem: 好的,除了其他方面,您的代码还存在以下问题:

在此处输入图片说明

I commented out the bits we didn't have yet, and it worked: 我注释掉了我们还没有的部分,并且有效:

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

I dunno why you're calling questionOneAnswer(); 我不知道为什么你要打电话给questionOneAnswer(); a second time (once inside score - where you assign the value to a temporary variable) and once again in the main where you don't even assign the boolean returned to anything. 第二次(一次在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;
}

Most of this is garbage. 其中大部分是垃圾。 Each time you call score() current, old and new are all set to 0. 每次您调用score()时,当前,新旧都设置为0。

Something like: 就像是:

static int runningTotal = 0;

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

Or kill two birds with one stone: 或用一块石头杀死两只鸟:

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

Also the default in questionOneAnswer(); 也是默认的questionOneAnswer(); shouldn't be true, it should be false, then you can fix the problem I led out with (bogus input will be false). 不应该为真,应该为假,然后您可以解决我导致的问题(虚假输入将为假)。 But even more than that we can boil the method down too: 但是,不仅如此,我们还可以简化该方法:

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

I didn't look at the input method. 我没有看输入法。

TLDR: commenting out the two superfluous lines in the main method and whatever it was that randomgen thinks it's doing seems to have fixed the problem. TLDR:注释掉main方法中的两条多余的行,而randomgen认为它所做的一切似乎都已解决了该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Boolean 值始终返回 true - Boolean value always returns true 布尔值不会破坏语句 - Boolean doesn't break for statement 我无法将程序中找到的 boolean 结果的正确值分配给 boolean 变量并使用结果检查条件 - I am Not able to assign the correct value of boolean result found in the program , to a boolean variable and to check a condition with the result 如果满足条件,语句是否也不会总是消失? - If statement doesn't always go off even when conditions are meet? 当我声明一个double并分配一个值时,它返回一个整数值 - When I declare a double and assign a value it returns an integer value 为什么我不能在我的 If 语句中检查方法是否返回 true? - Why can't I check if a method returns true in my If statement? android中的条件语句中的布尔值? - boolean in a conditional statement on android? 如何使用 if 语句检查数组是否不包含值 - how to check if an array doesn't contain a value using an if statement 我将true分配给名为“ ready”的布尔值,但是当我调试时,我发现它在onResourceReady函数中为false - i assign true to a boolean value named “ready” but when i debug i saw it takes false in onResourceReady function 在条件语句中使用Boolean_val与Boolean.TRUE(Boolean_val) - Boolean_val vs Boolean.TRUE(Boolean_val) when used in a conditional statement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM