将记录和字符串作为参数传递给方法,以在布尔表达式中比较它们并返回布尔值(真/假)

时间:2018-10-24 00:19:43

标签: java if-statement methods record

这些布尔方法一直存在问题。 我将发布整个代码,以便您可以看得更好,但是无论我输入什么内容,它都会不断返回错误的布尔值,我并不真正理解为什么。我的程序编写的方式就是我们要求我们的演讲的方式。我已经将字段从Record (q1.correctAnswer)传递给方法 questionOneAnswer ,并且如果我的输入是b / B,我期望返回true,但是它不会。实际上总是评估if语句的第一行 如果(由于正确原因!! =答案)

 import java.util.Scanner;    
 import java.util.Random;
 class miniProject
 {
  public static void main(String[] args)
  {
    boolean check = false;
    String answer;
    int scr;

    Questions q1 = new Questions();
    q1.question = "How many oscars did the Titanic movie got?";
    q1.correctAnswer = "b";

    questionOne();//prints the question one 
    answer = giveMeAnswer();//stores the user input from the method giveMeAnswer to the string answer
    check = questionOneAnswer(q1.correctAnswer, answer);//stores a boolean answer from the questionOneAnswer method and passes an answer value wchich is the user input collected
    scr = score(check); //it store the score points from the score method int the int scr with a boolean check which stores a boolean answer from the questionOneAnswer method
    printScore(scr); //Prints the score method


    System.exit(0); // tells the OS that the code has been execude without any problems and the program has ended
}


 /////////////////////////////////////////////////////////////////////
//This method ask 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 

}
////////////////////////////////////////////////////
//Takes the answer from the user
public static String giveMeAnswer()
{
    String i = input("");

    return i;
}
 //////////////////////////////////////////////////
//Checks if the answer from question one is correct
public static  boolean questionOneAnswer(String correct, String answer)
{
    boolean question = false;

    if (correct != answer)
    {
        print("False, you don't get any points!");
        question = false;
    }
    else if (correct == answer)
    {
        print("You answer is correct");

        question = true;
    }
    else 
    {
        print("Please enter only the answer letters");
    }


    return question; 
}

//Metrhod that returns true if the answer is correct and random generated 
score is added to the user
public static int score(boolean qOneCheck)
{   
    int currentScore = 0, oldScore = 0, newScore = 0, random;
    random = randomGen(); 


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

    return newScore;
}  

//Method that prints the score
public static String printScore(int score)
{
    String score1;
    score1 = "Your score is " + score;
    print(score1);



    return score1;
}


//This method receives input from the user and stores it in String called answer
public static String input(String message)
{      
    Scanner scanner = new Scanner(System.in); 
    String answer;
    System.out.println(message);
    answer = scanner.nextLine();

    return answer; 

}

// This method at the moment prints "Interesting" after user input
public static int randomGen()
{
    Random score = new Random();
    int score1 = score.nextInt(10) +1;

    return score1;  


}

//print method
public static void print(String message)
{
    System.out.println(message);

    return;
}

}

   //Record of a type Question 
   class Questions
   {
     String question;
     String correctAnswer;
   }

编辑:我更新了代码,现在可以使用了。谢谢!我将其发布,以便其他人可以作为参考!

   public static  boolean questionOneAnswer(String correct, String answer)
   {
    boolean question = false;

    if (answer.equalsIgnoreCase("A") || answer.equalsIgnoreCase("C") || 
    answer.equalsIgnoreCase("D"))
    {
        print("False, you don't get any points!");
        question = false;
    }
    else if (correct.equalsIgnoreCase(answer))
    {
        print("You answer is correct");

        question = true;
    }
    else 
    {
        print("Please enter only the answer letters");
    }


    return question; 
   }

0 个答案:

没有答案
相关问题