首页 > 解决方案 > Android Studio 井字游戏

问题描述

免责声明,我仍然是菜鸟。我不知道为什么 c 在这个循环中总是 9,它应该是 1,然后每个循环加 1,每个数字,我检查它是否是一个整数,如果它是一个整数,我将打印符号O,如果没有错,打印 X。

整个检查数字是否为圆形并根据数字选择什么符号,有效,我的问题是每次迭代,计数器“c”出于某种原因是 9 无论如何,它甚至不会从 1 开始正如我所说

JAVA


import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;                   // I know I don't need all of these, it's just easier to have all here.
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  }

  public void handle (View v){
      String symbol1 = "O";
      String symbol2 = "X"; 
      for (int c = 1; c < 10; c++) { // 9 iterations loop
          ((TextView) findViewById(R.id.texto)).setText("c = " + c);      // just checking what c is in each iteration.
          v.setEnabled(false);                                           // removes option to select a box.
          Button b = (Button) v;                                        //making this so I can use setText.
          if ((c % 2) == 0) {                                          // checking to see if its a whole number.
              b.setText(symbol1);                                       //if round print O in the box.
          } else b.setText(symbol2);                                   //else print X in the box.
      }
  }
}

XML

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/app"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="108dp"
        android:layout_height="101dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:onClick="handle"
        android:text="INSERT"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="109dp"
        android:layout_height="101dp"
        android:layout_marginStart="24dp"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="16dp"
        android:onClick="handle"
        android:text="INSERT"
        app:layout_constraintStart_toEndOf="@+id/btn1"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn3"
        android:layout_width="108dp"
        android:layout_height="100dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:onClick="handle"
        android:text="INSERT"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/btn2"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn4"
        android:layout_width="108dp"
        android:layout_height="102dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:onClick="handle"
        android:text="INSERT"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn1" />

    <Button
        android:id="@+id/btn5"
        android:layout_width="109dp"
        android:layout_height="101dp"
        android:layout_marginStart="24dp"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="16dp"
        android:onClick="handle"
        android:text="INSERT"
        app:layout_constraintStart_toEndOf="@+id/btn4"
        app:layout_constraintTop_toBottomOf="@+id/btn2" />

    <Button
        android:id="@+id/btn6"
        android:layout_width="108dp"
        android:layout_height="103dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:onClick="handle"
        android:text="INSERT"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/btn5"
        app:layout_constraintTop_toBottomOf="@+id/btn3" />

    <Button
        android:id="@+id/btn7"
        android:layout_width="108dp"
        android:layout_height="101dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:onClick="handle"
        android:text="INSERT"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn4" />

    <Button
        android:id="@+id/btn8"
        android:layout_width="109dp"
        android:layout_height="101dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:onClick="handle"
        android:text="INSERT"
        app:layout_constraintEnd_toStartOf="@+id/btn9"
        app:layout_constraintStart_toEndOf="@+id/btn7"
        app:layout_constraintTop_toBottomOf="@+id/btn5" />

    <Button
        android:id="@+id/btn9"
        android:layout_width="108dp"
        android:layout_height="100dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:onClick="handle"
        android:text="INSERT"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn6" />

    <TextView
        android:id="@+id/texto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="336dp"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.526"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

标签: javaandroidxml

解决方案


是的,这是合乎逻辑的

您的handle功能将在每次点击时执行,这意味着c增加9直到每次点击单独到达。

如果您需要c在每次单击时增加一,则需要初始化c为全局变量并仅使用条件更改for循环。if

解决方案

public class MainActivity extends AppCompatActivity {

    private int c = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void handle (View v){
        String symbol1 = "O";
        String symbol2 = "X";
        if (c < 10){
            Log.i("TAG", "handle: c is : " + c);
            ((TextView) findViewById(R.id.texto)).setText("c = " + c);      // just checking what c is in each iteration.
            v.setEnabled(false);                                           // removes option to select a box.
            Button b = (Button) v;                                        //making this so I can use setText.
            if ((c % 2) == 0) {                                          // checking to see if its a whole number.
                b.setText(symbol1);                                       //if round print O in the box.
            } else b.setText(symbol2);
            c++;
        }
    }

推荐阅读