题目来源:《疯狂Java讲义》
![](https://img.haomeiwen.com/i5311380/2ace35809d56a377.png)
package study;
public class ArrayTest {
public static void main (String[] args){
int[] arr = {1,2,3,4,5,6,7,8,9};
for (int i: arr ) {
for (int j=1;j <= i;j++) {
if(j > 1){System.out.print(',');}
System.out.print(i+" X "+j+" = "+i*j);
}
System.out.print('\n');
}
}
}
![](https://img.haomeiwen.com/i5311380/217253700254b184.png)
package study;
public class ArrayTest {
public static void main (String[] args){
System.out.println("输入4");
int num = 4;
int line = num * 2 - 1;
for(int i = 0;i < num;i++){
for(int j = 0;j < line;j++){
int count = i * 2 + 1;
int space = (line - count) / 2;
if(j >= space && j < (space + count)){
System.out.print('*');
}else{
System.out.print(' ');
}
}
System.out.print('\n');
}
}
}
![](https://img.haomeiwen.com/i5311380/bcfec3e83f3d6f7d.png)
package study;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Gobang {
private static int BOARD_SIZE = 15;
private String[][] board;
public void initBoard(){
//初始化棋盘数组
board = new String[BOARD_SIZE][BOARD_SIZE];
//画出棋盘
for(int i=0;i<BOARD_SIZE;i++){
for (int j=0;j<BOARD_SIZE;j++){
board[i][j] = "╋";
}
}
}
//在控制台输出棋盘
public void printBoard(){
//打印
for (int i=0;i<BOARD_SIZE;i++){
for(int j=0;j<BOARD_SIZE;j++){
System.out.print(board[i][j]);
}
System.out.print('\n');
}
}
public static void main (String[] args)throws Exception{
Gobang gb = new Gobang();
gb.initBoard();
gb.printBoard();
//键盘输入
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String inputStr = null;
while((inputStr = br.readLine()) != null){
String[] posStrArr = inputStr.split(",");
int xPos = Integer.parseInt(posStrArr[0]);
int yPos = Integer.parseInt(posStrArr[1]);
gb.board[yPos-1][xPos-1] = "●";
gb.printBoard();
System.out.println("请输入您下棋的坐标,以x,y的格式:");
}
}
}
网友评论