在Java中匹配单词

时间:2016-10-21 03:13:50

标签: java

所以我想在我的Java Netbeans中创建一个匹配游戏的单词,如填字游戏,匹配单词和其他益智游戏。我在制作int =我存储在数组中的单词时遇到了麻烦。

String word; int numword=0;
    // Words that will be stores in my arry
    String [] myNames = {"Pie","Soccer","Chelsea","Gaming","Steam"};
    // User inputs the number
    numword = new Scanner(System.in).nextInt();
    // Print the word for testing purposes
    System.out.println(myNames[numword]);
    // Type the word 
    word = new Scanner(System.in).nextLine();
    // Check if the word is right
    if (word.equals(n)){
        System.out.println("You got it");

    }else{
        System.out.println("You got it wrong");
    }

2 个答案:

答案 0 :(得分:0)

我对你的代码做了一些修改。这是你想要的吗?

String word;
int numword = 0;
Scanner scanner = new Scanner(System.in);
// Words that will be stores in my arry
String[] myNames = {"Pie", "Soccer", "Chelsea", "Gaming", "Steam"};
// User inputs the number
numword = scanner.nextInt();
while (numword >= myNames.length || numword < 0) {
    System.out.println("Not a valid number, enter value between 0 and " + (myNames.length - 1));
    numword = scanner.nextInt();
}
// Print the word for testing purposes
System.out.println(myNames[numword]);
// Type the word
word = scanner.next();
// Check if the word is right
if (word.equals(myNames[numword])) {
    System.out.println("You got it");
} else {
    System.out.println("You got it wrong");
}

答案 1 :(得分:0)

您可以使用BufferedReader来获取用户输入:

import java.util.BufferedReader;

public class Main {
    public static void main (String[] args) {
        BufferedReader br = new BufferedReader (new InputStreamReader (System.in)));
        String input = br.readLine ();
    }
}