用Java利用集合类实现随机产生26个英文字母软键盘。
时间: 2024-02-28 16:54:27 浏览: 63
好的,下面是Java代码实现:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class RandomKeyboard {
public static void main(String[] args) {
List<Character> letters = new ArrayList<>();
for (char c = 'a'; c <= 'z'; c++) {
letters.add(c);
}
Collections.shuffle(letters);
char[][] keyboard = new char[3][9];
keyboard[0][0] = letters.get(0);
keyboard[0][1] = letters.get(1);
keyboard[0][2] = letters.get(2);
keyboard[0][3] = letters.get(3);
keyboard[0][4] = letters.get(4);
keyboard[0][5] = letters.get(5);
keyboard[0][6] = letters.get(6);
keyboard[0][7] = letters.get(7);
keyboard[0][8] = letters.get(8);
keyboard[1][0] = ' ';
keyboard[1][1] = letters.get(9);
keyboard[1][2] = letters.get(10);
keyboard[1][3] = letters.get(11);
keyboard[1][4] = letters.get(12);
keyboard[1][5] = letters.get(13);
keyboard[1][6] = letters.get(14);
keyboard[1][7] = letters.get(15);
keyboard[1][8] = ' ';
keyboard[2][0] = ' ';
keyboard[2][1] = letters.get(16);
keyboard[2][2] = letters.get(17);
keyboard[2][3] = letters.get(18);
keyboard[2][4] = letters.get(19);
keyboard[2][5] = letters.get(20);
keyboard[2][6] = letters.get(21);
keyboard[2][7] = letters.get(22);
keyboard[2][8] = ' ';
System.out.println("Random Keyboard:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 9; j++) {
System.out.print(keyboard[i][j] + " ");
}
System.out.println();
}
}
}
```
代码中,我们首先使用 `ArrayList` 存储所有的英文字母,然后使用 `Collections.shuffle()` 方法随机打乱字母的顺序。接着,我们按照软键盘的布局将字母填充到一个二维数组中,并输出软键盘。注意,空格也被视为一个键位。
阅读全文