0

我正在编写一个简单的谜语程序,它读取谜语文本文件,将其存储到 ArrayList 中,然后读取谜语答案的文本文件,并将其存储到 ArrayList 中。嗯,到目前为止一切顺利。我的问题是,每当用户回答其中一个谜语时,它应该为正确答案打分,而对于错误答案则不计分。我测试了它,正确地回答了这个谜语,但是程序却说它是错误的。现在,有些答案应该接受大于 1 作为正确答案,例如: 问:什么有眼睛但看不见?A:一个土豆,一场风暴,一根针。假设用户没有想到 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 英尺高而不打破它?
你怎么能只用一根棍子生火呢?
如何分辨一罐鸡汤和一罐番茄汤?
长颈鹿能生孩子吗?
什么有四个轮子和苍蝇?
喂我我就活,给我点东西喝我就死了。我是什么?
什么有眼睛却看不见?
什么时候门不是门?

示例答案文本
呼吸
他不想撞到酒吧
打开门,把他放进去,关上门
打开门,把长颈鹿拿出来,把他放进去,关上门
大象,他在冰箱
里 跳进去,游过去,出去。鳄鱼在开会
绳子只系在马上
8. 把上半部拿开,留下“o”
把它扔到 4 英尺处,前 3 英尺处鸡蛋不会碰到任何东西
确保它是一根火柴
阅读标签
不,他们有长颈鹿
垃圾箱

针、土豆、风暴或真正的情人
当它半开着

4

1 回答 1

2

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;
}
于 2013-10-20T17:41:50.463 回答