一个变量上的字符无法被 ‘==’ 符号检测到。

huangapple go评论83阅读模式
英文:

A char on a variable is not being detected by '==' symbol

问题

我花了几个小时来解决这个问题,但是没有成功。
我正在做一个基于二维数组的井字游戏,我使用了几个 "for" 循环来将 '-' 符号添加到数组的每个位置。
现在我正在尝试编写一个验证方法,以防止玩家将他的 'X' 放在已占用或无效的位置上。
当我打印棋盘/数组时,每个位置上都有 '-',但是当我尝试执行 "如果位置有 '-',则是有效的移动" 的代码时,代码始终返回没有 '-',即使实际上有。
"jog" 和 "tab" 是因为我正在使用不同的类。
此外,我的调试从第一个 println 开始就没有显示任何变量,我不知道为什么。

public static void main(String[] args) {
    Tabuleiro tab = new Tabuleiro();
    Jogador jog = new Jogador();
    Computador comp = new Computador();
    
    tab.inicializarTabuleiro();
    tab.mostraTabuleiro();
    
    System.out.println("Bem vindo ao Jogo da Velha!");
    System.out.println("Escolha a dificuldade desejada de 1 a 3");
    Scanner dificuldade = new Scanner(System.in);
    if (dificuldade.nextInt() == 1){
        for (int i = 1; i <= 9; i++){
            Scanner scan = new Scanner(System.in);
            System.out.println("Escolha uma posição(1-9)");
            int jogada = scan.nextInt();
            jog.converteJogada(jogada); //将键盘输入的整数转换为数组上的位置
            jog.verificaJogada(jogada); //这段代码有问题
            System.out.println(jog.row); //用于检查位置是否正确,是正确的
            System.out.println(jog.col);//与上面类似
            System.out.println(tab.mat[jog.row][jog.col]); //用于打印位置,以查看是否有 '-'
            System.out.println(jog.valid); //查看是否确认了 '-'
        } 
    }
}

public class Tabuleiro {
    // ...
}

public class Jogador {
    // ...
    public void verificaJogada(int jogada){
        if ((jogada >= 0 && jogada <= 9) && (tab.mat[row][col] == '-')){
            valid = true;
        }
        else {
            valid = false;
        }
    }
}

问题已解决,因为我接受了糟糕的课程,我有一个不良的实践习惯,即在每个类上都实例化其他类。这使得我的代码有很多数组,而不仅仅是一个数组在我的代码中被引用。因此,我的主要数组(例如 Tabuleiro)使用 '-' 进行了初始化,但在 Jogador 类中存在一个 Tabuleiro 类的实例,这个实例的数组没有被初始化,这就是我的 "verificaJogada" 方法在测试的数组。感谢回答帮助我解决了这个问题。

英文:

I'm here for hours trying to solve this problem but with no success.
I'm doing a Tic Tac Toe game based on an 2D array, and i used a couple of "for" to add '-' symbols to every position of the array.
Now i'm trying to do a validation method to prevent the player from putting his 'X' on a occupied or invalid position.
When i print the board/array the '-' is there on every position, but when i try to do an "if the position has '-' it's a valid movement", the code keeps returning that there's no '-' there, even if it has.
The "jog" and "tab" its because i'm using different class.
Also my debug doesn't shows any variable from the first println on, i dont know why.

public static void main(String[] args) {
Tabuleiro tab = new Tabuleiro();
Jogador jog = new Jogador();
Computador comp = new Computador();
tab.inicializarTabuleiro();
tab.mostraTabuleiro();
System.out.println(&quot;Bem vindo ao Jogo da Velha!&quot;);
System.out.println(&quot;Escolha a dificuldade desejada de 1 a 3&quot;);
Scanner dificuldade = new Scanner(System.in);
if (dificuldade.nextInt() == 1){
for (int i = 1; i &lt;= 9; i++){
Scanner scan = new Scanner(System.in);
System.out.println(&quot;Escolha uma posi&#231;&#227;o(1-9)&quot;);
int jogada = scan.nextInt();
jog.converteJogada(jogada); //this code converts int from keyboard to a position on array
jog.verificaJogada(jogada); //this code is the problem
System.out.println(jog.row); //used this to check if position is correct, it is
System.out.println(jog.col);//same from above
System.out.println(tab.mat[jog.row][jog.col]); //used to print the position to see if &#39;-&#39; it was there, it was
System.out.println(jog.valid); //to see if &#39;-&#39; is being confirmed or not
} 

public class Tabuleiro {
public Tabuleiro(){
}
public char mat[][] = new char[3][3];
public void inicializarTabuleiro(){
for(int i = 0; i &lt; 3; i++){
for(int j = 0; j &lt; 3; j++){
mat[i][j] = &#39;-&#39;;
}
}
}
public void mostraTabuleiro(){
System.out.println(&quot;-------------&quot;);
for (int i = 0; i &lt; 3; i++){
System.out.print(&quot;| &quot;);
for (int j = 0;j &lt; 3; j++){
System.out.print(mat[i][j] + &quot; | &quot;);
}	
System.out.println();
System.out.println(&quot;-------------&quot;);
}
}
}

public class Jogador{
public Jogador(){
}
public Jogador(int linha, int coluna, boolean valid) {
this.linha = linha;
this.coluna = coluna;
this.valid = valid;
}
Tabuleiro tab = new Tabuleiro();
Jogo jog = new Jogo();
public int linha;
public int coluna;
public boolean valid;
public char nulo;
public void converteJogada(int jogada){
switch(jogada){
case 1:
row = 0;
col = 0;
break;
case 2:
row = 0;
col = 1;
break; 
case 3:
row = 0;
col = 2;
break;
case 4:
row = 1;
col = 0;
break;
case 5:
row = 1;
col = 1;
break;
case 6:
row = 1;
col = 2;
break;
case 7:
row = 2;
col = 0;
break;
case 8:
row = 2;
col = 1;
break;
case 9:
row = 2;
col = 2;
break;
default:
break;}
} 
public void verificaJogada(int jogada){
if ((jogada &gt;= 0 &amp;&amp; jogada &lt;= 9) &amp;&amp; (tab.mat[row][col] == &#39;-&#39;)){
valid = true;
}
else {
valid = false;
}

}

PROBLEM SOLVED Because of poor lessons i was having the bad practice to instance the others classes on every class. This made my code have a lot of arrays and not just one were my code was referencing to. So my main array(e.g. the Tabuleiro) was initialized with the '-' but there was an instance of Tabuleiro class on Jogador class, the array of this instance was not being initialized and that was the array my "verificaJogada" was testing. Thanks for the answers that helped me achieve this.

答案1

得分: 1

你的问题的原因是你在类Jogador中声明了一个名为Tabuleiro的字段,就像这样:

Tabuleiro tab = new Tabuleiro();

而且这就是Jogador.verificaJogada正在测试的内容。但是这是一个与你在main方法中初始化的Tabuleiro实例不同的实例。

显然,verificaJogada需要测试你在main中创建并初始化的Tabuleiro


> 那么我怎么才能在那里引用到main方法中的实例呢?

你可以将在main中创建的Tabuleiro实例作为参数传递:

  • 要么作为传递给Jogador构造函数的参数(然后你可以将其赋值给Jogador.tab字段)
  • 要么作为传递给verificaJogada方法的参数。

> 我原以为我必须在每个类中都创建每个类的一个实例才能使其工作。。

我不知道你是从哪里得到这个想法的。每次你写new Tabuleiro()时,你都在创建一个与之前所有创建的实例都不同的新的Tabuleiro实例。

Java中的new运算符总是创建一个新的实例<sup>1</sup>。它从不返回之前创建的实例。


<sup>1 - ... 或者抛出异常。</sup>

英文:

The cause of your problems is that you have declared the class Jogador to have a Tabuleiro field like this:

Tabuleiro tab = new Tabuleiro();

and that is what Jogador.verificaJogada is testing. But that is a different Tabuleiro instance to the one that you initialized in your main method.

Clearly verificaJogada needs to be testing the Tabuleiro that you created and initialized in main.


> How can i reference the instance of main there then?

You pass the Tabuleiro instance created in main as a parameter:

  • either a parameter to the constructor for Jogador (which you can then assign to the Jogador.tab field)
  • or a parameter to the verificaJogada method.

> I thought i had to make an instance of every class on every class for this to work..

I don't know where you got that idea from. Each time you write new Tabuleiro(), you are creating a new Tabuleiro instance that is distinct from all previously created instances.

The Java new operator always creates a new instance<sup>1</sup>. It never returns an instance that was created earlier.


<sup>1 - ... or throws an exception.</sup>

huangapple
  • 本文由 发表于 2020年7月25日 11:21:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63083981.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定