我正在尝试用用户输入的单词填充数组。每个单词必须比前一个长一个字母,比下一个短一个字母。它们的长度等于表行索引,从 2 开始计数。单词最终会创建一个单面金字塔,例如:
A
AB
ABC
ABCD
Scanner sc = new Scanner(System.in);
System.out.println("Give the height of array: ");
height = sc.nextInt();
String[] words = new String[height];
for(int i=2; i<height+2; i++){
System.out.println("Give word with "+i+" letters.");
words[i-2] = sc.next();
while( words[i-2].length()>i-2 || words[i-2].length()<words[i-3].length() ){
words[i-2] = sc.next();
}
}
如何限制从扫描仪读取的单词以满足要求?目前 while 循环根本不影响扫描仪:/
这不是家庭作业。我正在尝试创建一个简单的应用程序,然后为它创建 gui。