Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为java问答游戏匹配2个问答列表_Java_String_If Statement_Arraylist - Fatal编程技术网

为java问答游戏匹配2个问答列表

为java问答游戏匹配2个问答列表,java,string,if-statement,arraylist,Java,String,If Statement,Arraylist,我正在用netbeans用java制作一个问答游戏。我为我的所有问题制作了一个数组列表,为我的答案制作了另一个数组列表。我将它们按顺序排列(比如第一名是安大略省的首府……而答案列表上的第一名是多伦多……所以它们都是匹配的) 我这样做是为了让它随机生成一个问题。。。但现在我必须让它与答案相匹配。。。。并检查用户的输入是否与答案匹配。这是我尝试过的,但不起作用: String input = inputTextField.getText(); String output = ""; question

我正在用netbeans用java制作一个问答游戏。我为我的所有问题制作了一个数组列表,为我的答案制作了另一个数组列表。我将它们按顺序排列(比如第一名是安大略省的首府……而答案列表上的第一名是多伦多……所以它们都是匹配的)

我这样做是为了让它随机生成一个问题。。。但现在我必须让它与答案相匹配。。。。并检查用户的输入是否与答案匹配。这是我尝试过的,但不起作用:

String input = inputTextField.getText();
String output = "";
questions = answers -------> Those are the names of my 2 array lists

if (input = answers) {
output = "Congratulations";
}
else if (input != answers) {
 output = "You got the question wrong";
}

**请帮忙。。。我也是初学者,谢谢:)

您需要使用
contains
方法检查
数组列表是否包含问题和答案

ArrayList<String> questions = new ArrayList<String>();
ArrayList<String> answers = new ArrayList<String>();

questions.add("What is you name?");
answers.add("My answer here");

String inputQuestion ="GET_QUESTION_FROM_TEXTFIELD_HERE";
String inputAnswer ="GET_ANSWER_FROM_TEXTFIELD_HERE";

String output = "";

if(questions.contains(inputQuestion))
{
    if(inputAnswer.equals(answers.get(questions.indexOf(inputQuestion))))
        output = "Congratulations";
    else
        output = "Worng answer";
}
else
    output = "You got the question wrong";
ArrayList questions=new ArrayList();
ArrayList answers=新的ArrayList();
问题。加上(“你叫什么名字?”);
答案。添加(“我的答案在此”);
String inputQuestion=“从此处的文本字段获取问题”;
String inputswer=“从此处的文本字段获取答案”;
字符串输出=”;
if(问题.包含(输入问题))
{
if(inputswer.equals(answers.get(questions.indexOf(inputQuestion)))
output=“恭喜”;
其他的
输出=“工作应答”;
}
其他的
output=“你把问题搞错了”;

如果
答案
是你的
数组列表
,那么你的If条件应该是

if (answers.contains(input)) {
    output = "Congratulations";
}

更好的方法是使用
java.util.Map
代替
对的两个列表。这样你就可以确定你的问题的正确答案


更高级的是,您甚至可以这样做
Map
,并且一个问题有多个答案是正确的。

创建一个包含问题和答案的新对象,并将其放入列表中。然后从该列表中随机获取一个对象并发布该对象的问题。然后获取输入并检查该对象的答案是否等于该对象中的答案。干杯

public class test {


    public static void main(String[] args) {
        ArrayList<QandA> QandAnswerCntainer = new ArrayList<QandA>();
        QandAnswerCntainer.add(new QandA("What is biggest flower","ROSE"));
        QandAnswerCntainer.add(new QandA("What is biggest ship","test"));

        Random r = new Random();
        int randomQ = r.nextInt(2);
        QandA testQuestionAndAnswer = QandAnswerCntainer.get(randomQ);

        System.out.println(testQuestionAndAnswer.getQuestion());

        //get the answer to the string using your input . I will make a demo answer
        String answer = "test"; // answer got from your input method

        if (answer.equals(testQuestionAndAnswer.getAnswer())){
            System.out.println("correct");
        }
        else {
            System.out.println("wrong");
        }




    }
}

class QandA{

    private String question;
    private String answer;

    QandA(String question,String answer){
        this.answer = answer;
        this.question = question;
    }


    public String getQuestion() {
        return question;
    }

    public String getAnswer() {
        return answer;
    }
}
公共类测试{
公共静态void main(字符串[]args){
ArrayList QandaswerContainer=新ArrayList();
添加(新的昆达(“什么是最大的花”,“玫瑰”));
添加(新的昆达(“什么是最大的船”,“测试”));
随机r=新随机();
int randomQ=r.nextInt(2);
QandA testQuestionAndAnswer=QandaSwerContainer.get(随机Q);
System.out.println(testQuestionAndAnswer.getQuestion());
//使用您的输入获取字符串的答案。我将制作一个演示答案
String answer=“test”//从输入法中获取答案
if(answer.equals(testQuestionAndAnswer.getAnswer())){
系统输出打印项次(“正确”);
}
否则{
System.out.println(“错误”);
}
}
}
班达{
私有字符串问题;
私有字符串应答;
QandA(字符串问题,字符串答案){
这个答案=答案;
这个问题=问题;
}
公共字符串getQuestion(){
返回问题;
}
公共字符串getAnswer(){
返回答案;
}
}

为什么不尝试使用HashMap呢。它将比arraylist容易得多。
answers.contains(input)
不测试它是否是正确的答案,只测试它是否是现有的答案。这可能是一个完全不同的问题的答案!