day3基本语句
循环,条件语句操作
杨辉三角
public class Chap01 {
// 杨辉三角(5)
public static void main(String[] args) {
int[][] temp = new int[5][5];
for (int i = 0; i < 5; i++) {
for (int j = 0; j <= i; j++) {
// 将左边第一列与第一行赋值为1
if (i == 0 || j == 0){
temp[i][j] = 1;
} else {
// 将上面的两个数相加
temp[i][j] = temp[i - 1][j - 1] + temp[i -1][j];
}
System.out.print(temp[i][j] + "\t");
}
System.out.println();
}
}
}
水仙花数
public class Chap02 {
// 水仙花数
public static void main(String[] args) {
for (int i = 100; i < 1000; i++) {
int bai = i / 100;
int shi = i % 100 / 10;
int ge = i % 10;
if (bai * bai * bai + shi * shi * shi + ge * ge * ge == i){
System.out.println(i);
}
}
}
}
九九乘法表
public class Chap03 {
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " * " + i + " = " + i * j + "\t");
}
System.out.println();
}
}
}
五子棋
public class Gobang {
public static char[][] chessboard = { // 棋盘的棋子数组
{'┌', '┬', '┬', '┬', '┬', '┬', '┬', '┬', '┬', '┬', '┬', '┐'},
{'├', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┤'},
{'├', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┤'},
{'├', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┤'},
{'├', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┤'},
{'├', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┤'},
{'├', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┤'},
{'├', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┤'},
{'├', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┤'},
{'├', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┤'},
{'├', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┼', '┤'},
{'└', '┴', '┴', '┴', '┴', '┴', '┴', '┴', '┴', '┴', '┴', '┘'}
};
private static final String separator = "─────"; // 棋盘构件
private static final Scanner scanner = new Scanner(System.in);
// 棋盘子的总个数
private static final int dropNo = chessboard.length * chessboard[1].length;
private static final char pieceA = '■'; // 棋子
private static final char pieceB = '▲';
public static void main(String[] args) {
showChessboard();
drop();
}
private static void showChessboard(){
// 列号
System.out.println("\t0 1 2 3 4 5 6 7 8 9 10 11");
for (int i = 0; i < chessboard.length; i++) {
System.out.print(i + "\t"); // 行号
for (int j = 0; j < chessboard[i].length; j++) {
if (j == chessboard[i].length - 1){
System.out.println(chessboard[i][j]); // 最后列不打印separator
}else {
System.out.print(chessboard[i][j] + separator);
}
}
if (i == chessboard.length - 1){
System.out.println();
}else {
System.out.println("\t│ │ │ │ │ │ │ │ │ │ │ │");
}
}
}
private static void drop(){
char choose;
outer:for (int i = 0; i < dropNo; i++) {
// 循环落子
System.out.print(i % 2 == 0 ? "请A玩家落子(x-y):": "请B玩家落子(x-y):");
choose = (i % 2 == 0) ? pieceA: pieceB; // 当前使用的棋子
while (true){
// 获取输入信息,判断是否为整数
if (scanner.hasNext()){ // 是否有输入
String str = scanner.next(); // 获取输入
String[] split = str.split("-");
if (split.length == 2){
int y = Integer.parseInt(split[0]); // 获得棋子坐标
int x = Integer.parseInt(split[1]);
if (x < chessboard[0].length && y < chessboard.length){
// 判断是否存在棋子
if (chessboard[x][y] == pieceA || chessboard[x][y] == pieceB){
System.out.print("非法落子,重新落子:");
}else {
chessboard[x][y] = choose; // 落子
break;
}
}else {
System.out.print("非法落子,重新落子:");
}
}
}else {
System.out.print("非法落子,重新落子:");
scanner.next(); // 清除scanner中的内容
}
}
showChessboard(); // 落子之后展示棋盘
for (int j = 0; j < chessboard.length; j++) {
for (int k = 0; k < chessboard[j].length; k++) {
// 水平方向
boolean case1 = (k + 4) < chessboard[j].length
&& chessboard[j][k] == choose
&& chessboard[j][k + 1] == choose
&& chessboard[j][k + 2] == choose
&& chessboard[j][k + 3] == choose
&& chessboard[j][k + 4] == choose;
// 垂直方向
boolean case2 = (j + 4) < chessboard.length
&& chessboard[j][k] == choose
&& chessboard[j + 1][k] == choose
&& chessboard[j + 2][k] == choose
&& chessboard[j + 3][k] == choose
&& chessboard[j + 4][k] == choose;
// 45°角
boolean case3 = (j + 4 < chessboard.length) && (k > 4)
&& chessboard[j][k] == choose
&& chessboard[j + 1][k - 1] == choose
&& chessboard[j + 2][k - 2] == choose
&& chessboard[j + 3][k - 3] == choose
&& chessboard[j + 4][k - 4] == choose;
// 135°角
boolean case4 = (j + 4 < chessboard.length) && (k + 4 < chessboard[j].length)
&& chessboard[j][k] == choose
&& chessboard[j + 1][k + 1] == choose
&& chessboard[j + 2][k + 2] == choose
&& chessboard[j + 3][k + 3] == choose
&& chessboard[j + 4][k + 4] == choose;
if (case1 || case2 || case3 || case4){
System.out.print(i % 2 == 0 ? "玩家A胜利": "B玩家胜利");
break outer; // 结束整个循环
}
}
}
}
}
}