[英]Boolean always returns whatever value I assign when initialised, but it doesn't check the conditional statement
我花了两个小时试图找出为什么布尔表达式始终返回从头开始分配的任何值,而不是从正确的条件语句中获取值。 在这种情况下,即使我的输入是A
或C
或D
,来自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.