由于某种原因,TicTacToeBoard类中的Move()函数无法正常工作。
这是主要的:
import java.util.*;
public class TicTakToe {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("What player are you(X or O)?");
String player = in.next();
System.out.println("What row do you want to play in(top, middle, bottom)?");
String row = in.next();
System.out.println("What column do you want to play in(left, center, right)?");
String column = in.next();
System.out.println(player + ", " + row + ", " + column);
TicTacToeBoard one = new TicTacToeBoard(player, row, column);
one.Move();
one.printBoard();
int counter = 1;
while(counter <= 9){
System.out.println("What player are you(X or O)?");
player = in.next();
System.out.println("What row do you want to play in(top, middle, bottom)?");
row = in.next();
System.out.println("What column do you want to play in(left, center, right)?");
column = in.next();
System.out.println(player + ", " + row + ", " + column);
one.Move();
counter ++;
one.printBoard();
}
}
}
这是班级:
class TicTacToeBoard {
private int[][]board = {
{0,0,0},
{0,0,0},
{0,0,0} };
String row, column, player;
//Constructor
public TicTacToeBoard(String r, String c, String p){
row = r;
column = c;
player = p;
}
public void Move(){
if (row == "top" && column == "left" && player == "X"){
board[0][0] = 1;
}
if (row == "top" && column == "center" && player == "X"){
board[0][1] = 1;
}
if (row == "top" && column == "right" && player == "X"){
board[0][2] = 1;
}
if (row == "middle" && column == "left" && player == "X"){
board[1][0] = 1;
}
if (row == "middle" && column == "center" && player == "X"){
board[1][1] = 1;
}
if (row == "middle" && column == "right" && player == "X"){
board[1][2] = 1;
}
if (row == "bottom" && column == "left" && player == "X"){
board[2][0] = 1;
}
if (row == "bottom" && column == "center" && player == "X"){
board[2][1] = 1;
}
if (row == "bottom" && column == "right" && player == "X"){
board[2][2] = 1;
}
if (row == "top" && column == "left" && player == "Y"){
board[0][0] = 1;
}
if (row == "top" && column == "center" && player == "Y"){
board[0][1] = 1;
}
if (row == "top" && column == "right" && player == "Y"){
board[0][2] = 1;
}
if (row == "middle" && column == "left" && player == "Y"){
board[1][0] = 1;
}
if (row == "middle" && column == "center" && player == "Y"){
board[1][1] = 1;
}
if (row == "middle" && column == "right" && player == "Y"){
board[1][2] = 1;
}
if (row == "bottom" && column == "left" && player == "Y"){
board[2][0] = 1;
}
if (row == "bottom" && column == "center" && player == "Y"){
board[2][1] = 1;
}
if (row == "bottom" && column == "right" && player == "Y"){
board[2][2] = 1;
}
}
public void printBoard(){
for(int i = 0;i <= 2;i++){
for(int j = 0; j <= 2;j++){
System.out.print(board[i][j]);
}
System.out.println();
}
}
}
输出结果为:
你是哪个球员(X或O)?
X
你想在哪一排(顶部,中部,底部)?
顶
您想要播放哪一列(左,中,右)?
左
X,顶部,左侧
000 000 000
你是哪个球员(X或O)?
出于某种原因,无论我输入什么,董事会最终会全部为零。关于我做错了什么的想法?
答案 0 :(得分:2)
我认为首先您需要使用row == "top"
方法替换String.equals(String)
和其他字符串比较
答案 1 :(得分:2)
代码中的第一个问题是在TicTacToeBoard
的构造函数中TicTacToeBoard(String r,String c,String p)期望行,列和播放器的contstructor参数但是你正在传递
TicTacToeBoard one = new TicTacToeBoard(player, row, column);
播放器,行和列哪个错了。
第二件事是你没有在你的while循环中更新TicTacToeBoard类的播放器,行,列值。每次当你在while循环中从用户那里获取输入时,你必须在TicTacToeBoard类中设置这些值。
答案 2 :(得分:0)
首先,它们在Java中被称为方法,而不是函数。
并将row == "...."
更改为row.equals(...)
,因为这是我们比较java中字符串的方式。
应该这样做。
答案 3 :(得分:0)
替换
之类的行 if (row == "top" && column == "left" && player == "X"){
board[0][0] = 1;
}
与
if ("top".equalsIgnoreCase(row) && "left".equalsIgnoreCase(column) && "X".equalsIgnoreCase(player)){
board[0][0] = 1;
}
==运算符询问这两个项是否是同一个对象..不论它们是否具有相同的值。