我不太确定,问题出在哪里。代码工作正常,但是,在我输入几个词后,即:
猫
猫
结尾
它向我展示了:
猫
猫
无效的。
我的猜测是字符串 k 或数组的大小。所以我想,我需要以某种方式输入“结束”工作,对吗?
public static void main(String[] args) {
Scanner sc = new Scanner(System. in );
String type;
String word;
String words[] = new String[100];
int i = 1;
String k = "end";
System.out.println("Type a word: ");
word = sc.next();
while (word.compareToIgnoreCase(k) != 0) {
words[i] = word;
i = i + 1;
word = sc.next();
}
System.out.println("What type A,B,C:");
typ = sc.next();
if (typ.equals("B")) {
int lenght = words.length;
lenght = i + 1;
i = 1;
while (i < lenght) {
System.out.print(words[i]);
System.out.println();
i = i + 1;
}
}
}
}