gpt4 book ai didi

java - 类方法不起作用

转载 作者:行者123 更新时间:2023-12-01 18:23:17 28 4
gpt4 key购买 nike

由于某种原因,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):

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,顶部,左侧

000000000

你是什么玩家(X或O)?

由于某种原因,无论我输入什么,主板最终都会全为零。关于我做错了什么有什么想法吗?

最佳答案

我认为首先您需要使用 String.equals(String) 方法替换 row == "top" 和其他字符串比较

关于java - 类方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27044988/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com