在selectline中输入的数字在板的第二行都得到了很好的实现。但是它不能从第三行开始工作。我该怎么办?以下是我的代码的一部分
如下图所示,它应该打印在第三行,第四行...我只打印到第二行。enter image description here
public class Practice1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Board board = new Board(13, 6);
SelectLine selLine = new SelectLine();
for (int i = 0; i < 10; i++) {
selLine.input();
board.put(selLine);
}
}
}
public class SelectLine {
int line;
public void input() {
System.out.print("Select Line : ");
Scanner sc = new Scanner(System.in);
line = sc.nextInt();
}
public int getY() {
return line;
}
}
public class Board {
char[][] string;
char[][] om;
int width;
int height;
int count;
public Board(int width, int height) {
count = 0;
string = new char[height][width];
this.width = width;
this.height = height;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (i == 0 && j == 0) {
string[i][j] = '┌';
} else if (j % 2 == 1) {
string[i][j] = '─';
} else if (i == 0 && j == width - 1) {
string[i][j] = '┐';
} else if (i == height - 1 && j == 0) {
string[i][j] = '└';
} else if (i == height - 1 && j == width - 1) {
string[i][j] = '┘';
} else if (i == 0) {
string[i][j] = '┬';
} else if (j == 0) {
string[i][j] = '├';
} else if (i == height - 1) {
string[i][j] = '┴';
} else if (j == width - 1) {
string[i][j] = '┤';
} else
string[i][j] = '┼';
}
}
}
public void print() {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++)
System.out.print(string[i][j]);
System.out.println();
}
}
public void put(SelectLine setLine) {
int y = setLine.getY();
int x = 6;
if (count % 2 == 0) {
if (string[x - 1][(y - 1) * 2] == '●' || string[x - 1][(y - 1) * 2] == '○') {
x -= 1;
string[x - 1][(y - 1) * 2] = '●';
count++;
} else {
string[x - 1][(y - 1) * 2] = '●';
count++;
}
} else {
if (string[x - 1][(y - 1) * 2] == '●' || string[x - 1][(y - 1) * 2] == '○') {
x -= 1;
string[x - 1][(y - 1) * 2] = '○';
count++;
} else {
string[x - 1][(y - 1) * 2] = '○';
count++;
}
}
this.print();
}
}
}发布于 2021-05-08 03:37:28
这不会是你正在寻找的答案,但希望能为你指明正确的方向。
// At this point Y = the selected column
// and X = 6 (presumably the end/bottom)
if (count % 2 == 0) {
if (string[x - 1][(y - 1) * 2] == '●' || string[x - 1][(y - 1) * 2] == '○') {
x -= 1; // Right here X is now 5
string[x - 1][(y - 1) * 2] = '●'; // and now you're setting string[4][]
count++;
} else {
string[x - 1][(y - 1) * 2] = '●'; // and now you're setting string [5][]
count++;
}
}你明白了吗,你永远不能设置``string0 0..3,哪4行是前4行?如果你这样做会发生什么:
// Only determine the piece in one spot.
// Because after this point it doesn't matter what colour it is
// you're just going to find the next open spot from the bottom up
char piece = (count %2 == 0) ? 'r' : 'w';
// Now check all your lines from the bottom
for (int x = 6; x < 0; x--) {
// does !string [x-1][y calc] contain 'r' or 'w'
// string [x-1][y calc] = piece;
// break;
}
// print/wins/etc.从底部开始,一直向上,直到该点不包含r或w,然后将其放在那里。
https://stackoverflow.com/questions/67439672
复制相似问题