简体   繁体   中英

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. 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.

      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. 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(); 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.

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.

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(); 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM