繁体   English   中英

将用户输入与java中的字符串数组匹配?

[英]Matching user input against a string array in java?

我对 Java 非常陌生,并完成了以下任务:

  • 编写一个 Java 程序来提示用户输入 3 个字母的正文部分名称,该名称必须在 3 个字母正文部分的“官方”列表中。 (手臂、耳朵、眼睛、牙龈、臀部、下巴、腿、唇、肋骨、脚趾)
  • 如果用户正确猜测,则将正确猜测显示为列表的一部分。
  • 允许用户继续猜测,直到他们猜到全部 10 个。
  • 如果正文部分不正确,则显示适当的消息。
  • 显示他们做出的猜测次数,包括正确的猜测次数。

给出的建议是在适当的情况下使用数组和集合以及异常处理,但我不知道从我目前编码的内容开始。 任何帮助将不胜感激,谢谢。

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    String[] bodyparts = new String [10];

    bodyparts[0] = "Arm";
    bodyparts[1] = "Ear";
    bodyparts[2] = "Eye";
    bodyparts[3] = "Gum";
    bodyparts[4] = "Hip";
    bodyparts[5] = "Jaw";
    bodyparts[6] = "Leg";
    bodyparts[7] = "Lip";
    bodyparts[8] = "Rib";
    bodyparts[9] = "Toe";

    Set<String> bodypartSet = new TreeSet<>();
    Collections.addAll(bodypartSet, bodyparts);

    System.out.println("Please enter a 3 letter body part: ");
    String bodypart = input.nextLine();

    if (bodypartSet.contains(bodypart)) {
        System.out.println("Correct,  " + bodypart + " is on the list!");
    } else {
        System.out.println("Nope, try again!");
    }

}

有很多方法可以做到这一点。 以下不是最好或最有效的,但它应该有效......首先,你必须将你的“官方”列表放在一个结构中,比如一个数组:

private static String[] offList={Arm, Ear, Eye, Gum, Hip, Jaw, Leg, Lip, Rib, Toe};

现在你必须编写一个方法来在“offList”中找到一个世界,就像这样:

private static boolean find(String word){
    for( int i=0; i<offList.length; i++){
        if(word.equals(offList[i])) //if "word" is in offList
            return true;
    }
    return false;
}

现在,让我们创建这个猜谜游戏 GUI:

public static void main(String[] args){
    LinkedList<String> guessed=new LinkedList<>();
    String s;
    Scanner input = new Scanner(System.in);
    while(guessed.size()<offList.length){
        System.out.println("Guessed= "+guessed.toString()); //you have to change it, if you want a better look
        System.out.print("Try:");
        s=input.nextLine();
        /*Here we ask to the user the same thing, unless the guessed list 
        contains all the words of offList.
        Every time we print the guessed worlds list*/ 
        if(find(s)){
            System.out.println("This world is in offList!");
            if(!guessed.contains(s)) //the world is counted only one time!
                guessed.add(s);
        }else
            System.out.println("Sorry...");
    }
    System.out.println("The complete list is "+guessed.toString());

}

如果你想在一个窗口中展示这个游戏,你应该学习一些 Java Swing 类。

编辑:我在主要帖子编辑之前发布我的答案。 首先你要了解Collections的优点和用法……当你知道了所有的LinkedList方法之后,比如这个赋值看起来就是个笑话! ;)

你需要一个循环,否则它只会要求输入一次。

这样的事情应该做:

    ArrayList<String> bodyParts = new ArrayList<String>();

    bodyParts.add("Arm");
    bodyParts.add("Ear");
    bodyParts.add("Eye");
    bodyParts.add("Gum");
    bodyParts.add("Hip");
    bodyParts.add("Jaw");
    bodyParts.add("Leg");
    bodyParts.add("Lip");
    bodyParts.add("Rib");
    bodyParts.add("Toe");
    String input = "";
    int totalGuesses = 0;

    Scanner sc = new Scanner(System.in);

    System.out.println("Start guessing...");

    while (!bodyParts.isEmpty()) {
        totalGuesses++;
        input = sc.nextLine();
        if (input.length() != 3 || !bodyParts.contains(input)) {
            // incorrect, do nothing
            System.out.println("Nope.");
        } else {
            // correct, remove entry
            bodyParts.remove(input);
            System.out.println("Correct! " + (10 - bodyParts.size()) + " correct guess" + ((10 - bodyParts.size()) != 1 ? "es" : ""));
        }
    }
    System.out.println("Done. You have found them all after " + totalGuesses + " guesses.");
    sc.close();

此外,这是区分大小写的。 输入arm时不会找到Arm 如果您需要所有猜测的数量,您可以简单地在循环之前添加一个int并在内部增加它。

我的例子的结果:

开始猜...
手臂
不。
手臂
正确的! 1 正确猜测
手臂
不。
耳朵
正确的! 2 正确猜测
眼睛
正确的! 3个正确的猜测

(……)

肋骨
正确的! 9个正确的猜测
脚趾
正确的! 10个正确的猜测
完毕。 经过 12 次猜测,您已全部找到。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM