这是又一篇记录Java习题的blog。
public class ChessBoard {
public static void main(String[] args) {
String chess[][] = new String[5][5];// 声明一个二维数组
Random random = new Random();// 创建随机数对象
Scanner scanner = new Scanner(System.in);// 创建Scanner对象
int k = random.nextInt(5);// 生成5以内的随机数
for (int i = 0; i < chess.length; i++) {// 设置棋盘内容
for (int j = 0; j < chess.length; j++) {
chess[i][j] = "O";
chess[i][k] = "X";
}
}
System.out.println("输入横坐标:");
int x = scanner.nextInt();// 接收横坐标
System.out.println("输入纵坐标:");
int y = scanner.nextInt();// 接收纵坐标
if (chess[x - 1][y].equals("O") && chess[x + 1][y].equals("O") && chess[x][y - 1].equals("O")
&& chess[x][y + 1].equals("O")) {// 判断是否周围全是O
System.out.println("挑战成功");
} else {
System.out.println("挑战失败");
}
}
}
public class DimensionsArray {
public static void main(String[] args) {
String[][] info = { { "admin", "123" }, { "tom", "223" }, { "jack", "113" } };// 声明一个String类型的二维数组
Scanner scanner = new Scanner(System.in);// 创建Scanner对象
int count = 0;// 记录登录错误的次数
boolean flag = true;// 设立一个标记
while (count < 3 && flag == true) {
System.out.println("Input the username:");
String username = scanner.nextLine();// 接收输入的用户名
System.out.println("Input the password:");
String password = scanner.nextLine();// 接收输入的密码
for (int i = 0; i < info.length; i++) {
if (info[i][0].equals(username) && info[i][1].equals(password)) {
flag = false;
System.out.println("登陆成功");
break;
} else {
System.out.println("用户名|密码错误");
count++;
break;
}
}
}
if (count == 3) {
System.out.println("已超过最大登录次数");
}
}
}
public class Fibonacci {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.print(new Fibonacci().sequence(i) + " ");
}
}
public int sequence(int n) {
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}
return sequence(n - 1) + sequence(n - 2);
}
}
public class IsSort {
public static void main(String[] args) {
int[] num = { 0, 1, 5, 3, 5, 6 };// 定义一个int型数组
System.out.println(new IsSort().sort(num, 6));
}
public boolean sort(int[] a, int n) {
if (n == 1 || n == 0) {
return true;
}
if (a[n - 1] < a[n - 2]) {// 是否为升序
return false;
}
return sort(a, n - 1);
}
}
public class IsPrime {
public static void main(String[] args) {
Random random = new Random();// 声明一个随机数对象
int number = random.nextInt(20) + 1;// 随机生成一个10以内的数
new IsPrime().JudgePrime(number);
}
public void JudgePrime(int a) {
if (a == 1) {
System.out.println(a + " is a prime.");
}
int i = 2;
for (; i < a; i++) {
if (a % i == 0) {
System.out.println(a + " is not a prime.");
break;
}
}
if (a == i) {
System.out.println(a + " is a prime.");
}
}
}
public class ArrayOperation {
public static void main(String[] args) {
int[] number = new int[10];// 声明一个长度为10的int型数组
Random random = new Random();// 定义一个随机数对象
int key;
for (int i = 0; i < number.length; i++) {
do {
key = random.nextInt(20) + 1;
} while (isExist(number, key));
number[i] = key;
System.out.print(number[i] + " ");
}
}
/**
* 判断数组中是否已存在某数值
*
* @param a
* @param key
* @return
*/
public static boolean isExist(int[] a, int key) {
for (int i = 0; i < a.length; i++) {
if (a[i] == key) {
return true;
}
}
return false;
}
}
OK,今天的习题记录结束了,继续加油吧!