【发布时间】:2012-12-27 21:10:59
【问题描述】:
我不太确定,问题出在哪里。 代码工作正常,但是在我输入几个词后,即:
猫
猫
结束
它告诉我:
猫
猫
空。
我的猜测是字符串 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;
}
}
}
}
【问题讨论】:
-
当然,还有两个选项可以格式化文本 A 和 C。但我现在想专注于那个“空”的东西。
-
一个建议:在发布
int lenght = words.lenght;之前尝试清理代码什么都不做(lenght在下一行被覆盖)。而正确的拼写是length(虽然这和编程无关)
标签: java arrays loops while-loop