【问题标题】:Is there anyway to match answers and questions in String array无论如何要匹配字符串数组中的答案和问题
【发布时间】:2019-01-06 23:34:06
【问题描述】:

我正在尝试编写文字游戏。有些问题是随机显示的,用户尝试正确回答。

我用 switch case 匹配了问题和答案,但我无法检查它是否正确,因为我不知道如何在字符串数组中找到单词。此外,由于该方法,switch-case 中存在错误。

import java.util.Scanner;

import java.util.Random;

public class Odev {

public static void main(String[] args) {

    Scanner input= new Scanner(System.in);
    System.out.println("Enter the 1 for answer or 2 for requesting a letter.");


    int first= input.nextInt();
    String[] question = new String[9];
    String [] answer=new String[9];
    question=array(question);
    answer=array2(answer);
    Random b= new Random();
    int randomNumber=b.nextInt(question.length);


    if(first==1) {
        System.out.println(question[randomNumber]);

        System.out.println(randomNumber);
        String a1=input.next();
        switch (randomNumber) {
        case 0: equalM(a1, answer[0]);

        break;
        case 1: equalM(a1, answer[1]);

        break;
        case 2:equalM(a1, answer[2]);

        break;
        case 3:equalM(a1, answer[3]);

        break;
        case 4:equalM(a1, answer[4]);

        break;
        case 5:equalM(a1, answer[5]);

        break;
        case 6:equalM(a1, answer[6]);

        break;
        case 7:equalM(a1, answer[7]);

        break;
        case 8:equalM(a1, answer[8]);
        break;
        }
    }


}

public static String [] array(String [] question) {
    question[0]="A beverage which is white and generally consumed in mornings";
    question[1]="The natural satellite of earth";
    question[2]="An adjective which describes people who have not adequate money";
    question[3]="A furniture sit around for eating or studying";
    question[4]="A group that consists a lot of soldier";
    question[5]="A fruit which can be red, yellow and green";
    question[6]="A tool for writing which consists graphite";
    question[7]="A beverage which is consumed by people who need caffeine ";
    question[8]="A term which is stand for the smallest group of the    society  ";
    return question;        
}

public static String [] array2(String [] answer) {  
    answer[0]="milk";
    answer[1]="moon";
    answer[2]="poor";
    answer[3]="table";
    answer[4]="army";
    answer[5]="apple";
    answer[6]="pencil";
    answer[7]="coffee";
    answer[8]="family";
    return answer;
}

public static String[] equalM(String a1, String[] answer) {
     for (int i=0; i<answer.length; i++) {
         if(a1.equals(answer[i])) {
             System.out.println("Correct you gained 500 points");
         } else 
             System.out.println("Wrong.You lost 500 points");
     }
     return answer;
}

}

【问题讨论】:

  • 那个开关没用。您的代码等同于 equalM(a1, answer[randomNumber]); - 至于您的 equalM 方法本身;在宣布答案错误(或正确)之前检查所有的答案。照原样,您对照所有的答案检查每个答案(并报告结果)。那是不对的。您可能也应该传递问题编号......否则,以正确方式的错误答案被视为正确。仔细想想,你只需要a1.equals(answer[randomNumber])
  • 我知道它没用,但我不能做答案字符串的事情,我想不通。所以,我来这里是为了寻找可以提供帮助的人。

标签: java arrays string random


【解决方案1】:

您可以使用哈希图轻松完成此操作,但如果您想坚持使用数组,您可以打印出所有答案选项及其索引号,然后读取用户输入的数字以获得正确答案.然后,您只需检查该数字是否等于用于生成问题的随机数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-08
    • 1970-01-01
    • 2014-11-03
    • 2015-07-23
    • 1970-01-01
    • 2010-09-20
    • 1970-01-01
    • 2011-11-25
    相关资源
    最近更新 更多