我的布尔总是出现正确 [英] My boolean always comes up correct

查看:140
本文介绍了我的布尔总是出现正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我下面的一段code的我期待比较一块文本文件。
然而,由于某种原因,不论是什么的值的用户类型上来
正确的。

我想,无需担心其案件或任何比较值的
开头或结尾空格。

  //显示的问题给用户
的System.out.println(问:+ myList.get(random1));//接受用户输入
System.out.print(请输入您的答案是:);//存储在一个变量,但小写用户回答
。答案= scanner.nextLine()与toLowerCase();
的System.out.println();//显示从ArrayList中正式正确答案
的System.out.println(答:+ myList.get(random1 +1));//如果用户的答案,官方的回答一致,告诉他们,他们是
//正确的,奖励积分
//别人告诉他们,他们吮吸LOL
如果(myList.get(random1 + 1)。载有(回答)== FALSE){
    的System.out.println(正确!);
    totalScore = totalScore + awardedPoints;
    的System.out.println(你赢了+ awardedPoints);
}
其他{
    的System.out.println(你suckkkkkkk);
}//显示总累计积分
的System.out.println(你总要点是:+ totalScore);//等待用户显示下一个问题之前,按任意键
System.out.print(打回车);
scanner.nextLine();


解决方案

试试这个。

  //显示从ArrayList中正式正确答案
串correctAnswer = myList.get(random1 +1);
的System.out.println(答:+ correctAnswer); //而是使用一个变量//如果用户的答案,官方的回答一致,告诉他们,他们是
//正确的,奖励积分
//别人告诉他们,他们吮吸LOL
如果(correctAnswer.equalIgnoreCase(回答)){//效率比contains()方法
    的System.out.println(正确!);
    totalScore = totalScore + awardedPoints;
    的System.out.println(你赢了+ awardedPoints);
}
其他{
    的System.out.println(你suckkkkkkk);
}

In my following piece of code I am looking to compare a piece of a text file. However, for some reason, no matter what the user types in the value comes up correct.

I'm trying to compare the value without worrying about its case or about any leading or trailing white space.

// Display the question to the user
System.out.println("Question: " + myList.get(random1));

// Accept user input
System.out.print("Please type your answer: ");

// Store the user answer in a variable but lowercase
answer = scanner.nextLine().toLowerCase();
System.out.println();

// Display the officially correct answer from the arraylist
System.out.println("Answer: " + myList.get(random1 +1));

// if the user answer matches the official answer, tell them they're
// correct and award points
// else tell them they suck LOL
if(myList.get(random1 + 1).contains(answer) == false) {
    System.out.println("Correct!");
    totalScore = totalScore + awardedPoints;
    System.out.println("You won " + awardedPoints);
}
else {
    System.out.println("You suckkkkkkk");
}

// Display total accumulated points
System.out.println("Your total points are: " + totalScore);

// Wait for user to hit any key before displaying the next question
System.out.print("Hit Enter");
scanner.nextLine();

解决方案

Try this.

// Display the officially correct answer from the arraylist
String correctAnswer =  myList.get(random1 +1); 
System.out.println("Answer: " + correctAnswer); // Instead use a variable

// if the user answer matches the official answer, tell them they're
// correct and award points
// else tell them they suck LOL
if(correctAnswer.equalIgnoreCase(answer)) { // efficient than contains() method
    System.out.println("Correct!");
    totalScore = totalScore + awardedPoints;
    System.out.println("You won " + awardedPoints);
}
else {
    System.out.println("You suckkkkkkk");
}

这篇关于我的布尔总是出现正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆