我正在编写一个简单的谜语程序,它读取谜语的文本文件,将其存储到ArrayList中,然后读取谜语答案的文本文件,并将其存储到ArrayList中。到目前为止还不错。我的问题是,每当用户回答其中一个谜语时,它就应该给出一个正确答案的分数,而对于错误的答案却没有给出任何答案。我对它进行了测试,正确地回答了谜语,但是程序却说它错了。现在,一些答案应该接受超过1作为正确的答案,例如:问题:什么有眼睛但看不到?一个土豆,一场风暴,一针。假设用户没有想到3,而只是想到1,那就是风暴。我想要程序读风暴,因为它包含在答案中,那么它是正确的。这是我的密码。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
/* Array of riddles with a separate array of answers */
class RiddlesProgram
{
public RiddlesProgram(){} //Empty constructor
public void riddleGame() throws FileNotFoundException
{
String ridFile = "Riddles.txt";
String ansFile = "Answers.txt";
Scanner ridReader = new Scanner(new File(ridFile));
Scanner ansReader = new Scanner(new File(ansFile));
/** ArrayLists for riddles and answers */
ArrayList<String> riddles = new ArrayList<String>();
ArrayList<String> answers = new ArrayList<String>();
/** Reading the Riddles & storing them in their array */
while(ridReader.hasNext())
{
riddles.add(ridReader.nextLine());
}ridReader.close();
/** Reading the Answers & storing them in their array */
while(ansReader.hasNext())
{
answers.add(ansReader.nextLine());
}ansReader.close();
Scanner in = new Scanner(System.in);
String answer = "";
System.out.println("Welcome to the Riddler!");
System.out.println("Let's start answering riddles..."); System.out.println();
System.out.println("Each riddle will require either a one word answer, or multiple word answer.");
System.out.println("Example: \nQ: How much would could a wood chuck, chuck?\n"
+ "A: As much wood as a wood chuck could chuck if a wood chuck would chuck wood.");
int count = 1;
int points = 0;
while(count!=16)
{
System.out.println("Riddle # " + count);
System.out.println(riddles.get(count));
System.out.println("\nAnswer? ");
answer = in.nextLine();
if(answers.contains(answer.toLowerCase()))
{
System.out.println("Correct! + 1 point.");
points += 1;
}//End if
else
{
System.out.println("Wrong! No points!");
}//End else
count++;
}//End while
}//End riddlegame
}//End class
样本谜语
我比羽毛轻,但没有人能长时间地抱着我。我是什么?
三个人跑进酒吧,第四个人躲起来。他为什么躲起来?
你怎么把长颈鹿放在冰箱里?
你怎么把大象放在冰箱里?
所有的动物都去参加狮子王的会议。没有一只动物出现。哪种动物不来?
你来到了一条船上人住的河里。没有小船、木筏、桥梁,也没有材料来制造它们。你是怎么过关的?
一根十五英尺长的绳子系在马上。这匹马离一堆干草有25英尺远。这匹马怎么走到干草边?
你能从哪个号码取一半却什么也不留下?
你怎么能不打碎一个鸡蛋就掉3英尺呢?
你怎么能只用一根棍子生火呢?
你怎么能区分一罐鸡汤和一罐西红柿汤呢?
长颈鹿能生孩子吗?
什么东西有四个轮子和苍蝇?
喂我,我就活下去,给我点喝的,我就死定了。我是什么?
什么东西有眼睛却看不见?
什么时候门不是门?
样本答案文本
呼吸,呼吸
他不想去酒吧
打开门,把他放进去,关上门
开门,把长颈鹿弄出来,把他放进去,关上门
大象,他在冰箱里
跳进去,游过去,出去。接线员在会上
绳子除了马外什么都不系
发布于 2013-10-20 17:41:50
answers
不包含字符串"storm"
。它包含一个包含"storm"
的字符串。在检查answers
之前,从String#contains(String)
检索相应的字符串(而不是List#contains(Object)
,就像您现在所做的那样)。
String correctAnswer = answers.get(count).toLowerCase();
if(correctAnswer.contains(answer.toLowerCase()))
{
System.out.println("Correct! + 1 point.");
points += 1;
}
https://stackoverflow.com/questions/19480604
复制相似问题