简体   繁体   中英

Matching user input against a string array in java?

I am incredibly new to java and have been given the following task:

  • Write a Java Program to prompt a user for a 3 letter body part name which has to be in the 'official' list of 3 letter body parts. (Arm, Ear, Eye, Gum, Hip, Jaw, Leg, Lip, Rib, Toe)
  • If a user makes a guess correctly then display the correct guess as part of a list.
  • Allow the user to keep guessing until they have all 10.
  • If a body part is incorrect then display an appropriate message.
  • Display the number of guesses they have made including the correct ones.

The advice given was to use Arrays and Collections as well as Exception Handling where appropriate but I don't know where to go from what I've coded so far. Any help would be appreciated so much, thank you.

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!");
    }

}

There are a lot of way to do this. The following, isn't the best or the most efficient, but it should work... First of all, you have to put your "official" list in a structure, like an array:

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

Now you have to write a method that can find a world in that "offList", like that:

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;
}

Now, let's create this guessing game 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());

}

If you want to show this game in a window, you should have to study some Java Swing classes.

EDIT: I post my answer before the main post editing. First of all you have to understand the Collections advantages and usage... When you know all the LinkedList methods, for example, this assignment looks like a joke! ;)

You need a loop for that, otherwise it will only ask for input once.

Something like this should do:

    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();

Also, this is case sensitive. It will not find Arm when typing arm . And if you need the number of all guesses you can simply add an int before the loop and increase it inside.

The result of my example:

Start guessing...
arm
Nope.
Arm
Correct! 1 correct guess
Arm
Nope.
Ear
Correct! 2 correct guesses
Eye
Correct! 3 correct guesses

(...)

Rib
Correct! 9 correct guesses
Toe
Correct! 10 correct guesses
Done. You have found them all after 12 guesses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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