Java 将按钮中的值链接到公式时出现的问题

Java 将按钮中的值链接到公式时出现的问题,java,android,Java,Android,基本上,我不知道为什么它跳到主菜单时,我按下按钮,无论正确或错误的答案。没错,只有当答案错误时,它才应该跳转到主菜单。如果正确,它会显示一组新的方程。起初,我认为这可能是if语句中的条件问题,因为它会显示下一个等式,并在稍后跳出。但当我试图弄明白的时候,我意识到答案与问题无关。试着编译一下,你就会明白我的意思 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.

基本上,我不知道为什么它跳到主菜单时,我按下按钮,无论正确或错误的答案。没错,只有当答案错误时,它才应该跳转到主菜单。如果正确,它会显示一组新的方程。起初,我认为这可能是if语句中的条件问题,因为它会显示下一个等式,并在稍后跳出。但当我试图弄明白的时候,我意识到答案与问题无关。试着编译一下,你就会明白我的意思

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#e0e1c5"
android:id="@+id/background">

<TextView
    android:id="@+id/ready"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_marginTop="225dp"
    android:text="Good luck!"
    android:textColor="#000000"
    android:textSize="45dp"
    android:textStyle="bold"
    android:autoText="false"
     />

<TextView
    android:id="@+id/tapstart"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Tap to start..."
    android:textColor="#000000"
    android:textSize="27dp"
    android:textStyle="bold"
    android:autoText="false"

    android:layout_below="@+id/ready"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />


<TextView
    android:id="@+id/time"
    android:layout_width="130dp"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:gravity="center"
    android:text="00:00:00"
    android:textColor="#000000"
    android:textSize="25dp"
    android:textStyle="bold"
    android:autoText="false"
    android:layout_marginTop="55dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<TextView
    android:id="@+id/score"
    android:layout_width="130dp"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Score: 200"
    android:textColor="#000000"
    android:textSize="25dp"
    android:textStyle="bold"
    android:autoText="false"
    android:layout_below="@+id/time"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<TextView
    android:id="@+id/expression"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text=""
    android:textColor="#000000"
    android:textSize="45dp"
    android:textStyle="bold"
    android:autoText="false"
    android:layout_below="@+id/score"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="33dp" />

<Button
    android:id="@+id/ans1"
    android:layout_width="250dp"
    android:layout_height="60dp"
    android:gravity="center"
    android:textSize="25dp"
    android:textColor="#fff"
    android:text=""
    android:textStyle="bold"
    android:background="#2545cf"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/expression"
    android:layout_marginTop="37dp" />

<Button
    android:id="@+id/ans2"
    android:layout_width="250dp"
    android:layout_height="60dp"
    android:gravity="center"
    android:textSize="25dp"
    android:textColor="#fff"
    android:background="#eb131e"
    android:text=""
    android:textStyle="bold"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="5dp"
    android:layout_below="@+id/ans1"/>

<Button
    android:id="@+id/ans3"
    android:layout_width="250dp"
    android:layout_height="60dp"
    android:gravity="center"
    android:textSize="25dp"
    android:textColor="#fff"
    android:text=""
    android:textStyle="bold"
    android:background="#dacd10"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="5dp"
    android:layout_below="@+id/ans2"/>

</RelativeLayout>

}乍一看,我看到两个问题。

1.在
onClick()。所以,不管怎样,都要检查新值,而不是旧值。
修复起来很简单-只要将
if
语句移到
onClick()
方法开头的最后一个


2.您的最后一个
if
语句太复杂了,它总是返回
true
。让我们看一个例子:
num1=10,num2=4,ans=40,a={40115126},index1=0

第一个条件
(num1+num2)!=[index1]
将返回true!因为
10+2!=40
。因此,在任何情况下,所有这些巨额支票都将返回
true



为什么需要这么复杂的支票?

Game
类中有
ans
变量,单击了
View


做如下检查:
if(ans==Integer.parseInt(v.getText))
就是这样

你好,我的回答对你有帮助吗?没有,没有帮助,你自己试过了吗?它能工作吗?目前没有任何更新。你能把你的全部代码都发出来吗?老兄,我指出了你代码中的两个问题以及解决它们的方法。在代码中实现这一点。如果您仍然有问题,请发布您的更新代码,我将再次尝试提供帮助。
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.Random;

public class Game  extends Activity implements View.OnClickListener {

TextView t4,t5,t6,t7,t8;
Button b7,b8,b9;
RelativeLayout tapbg;

int num1 = 0, num2 = 0, ans = 0, x, y, z;
int []a = new int[3];
double []b = new double[3];
int index1, index2, index3;
double divideAns, divideAnsRound, l, m, i, j;

Random myrandom = new Random();

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

    t4=(TextView) findViewById(R.id.time);
    t5=(TextView) findViewById(R.id.score);
    t6=(TextView) findViewById(R.id.expression);

    b7=(Button) findViewById(R.id.ans1);
    b7.setOnClickListener(this);

    b8=(Button) findViewById(R.id.ans2);
    b8.setOnClickListener(this);

    b9=(Button) findViewById(R.id.ans3);
    b9.setOnClickListener(this);


    t7=(TextView) findViewById(R.id.ready);
    t8=(TextView) findViewById(R.id.tapstart);

    tapbg = (RelativeLayout)findViewById(R.id.background);
    tapbg.setOnClickListener(this);

    //hiding widgets
    t4.setVisibility(View.GONE);
    t5.setVisibility(View.GONE);
    t6.setVisibility(View.GONE);

    b7.setVisibility(View.GONE);
    b8.setVisibility(View.GONE);
    b9.setVisibility(View.GONE);
}

public void onClick(View v) {

    /*if (v == tapbg) {

        //disappearing ready statements
        t7.setVisibility(View.GONE);
        t8.setVisibility(View.GONE);

        //reappearing widgets
        t4.setVisibility(View.VISIBLE);
        t5.setVisibility(View.VISIBLE);
        t6.setVisibility(View.VISIBLE);

        b7.setVisibility(View.VISIBLE);
        b8.setVisibility(View.VISIBLE);
        b9.setVisibility(View.VISIBLE);


        //-----------------------------------------------------------------------------------------------------------------

        //-----------------------------------------------------------------------------------------------------------------

        tapbg.setOnClickListener(null); //disallowing tapbg to be clicked

    }*/

    //BUTTONS
    /*if ((v == b7 || v == b8 || v == b9) &&
        ((ans == a[index1]) || (ans == a[index2]) || (ans == a[index3]) ||
         (divideAnsRound == b[index1]) || (divideAnsRound == b[index2]) || (divideAnsRound == b[index3]))) {*/

    if ((v == b7 || v == b8 || v == b9 || v == tapbg) &&
            (((num1 + num2) == a[index1]) || ((num1 + num2) == a[index2]) || ((num1 + num2) == a[index3]) ||
             ((num1 - num2) == a[index1]) || ((num1 - num2) == a[index2]) || ((num1 - num2) == a[index3]) ||
             ((num1 * num2) == a[index1]) || ((num1 * num2) == a[index2]) || ((num1 * num2) == a[index3]) ||
             ((num1 / num2) == b[index1]) || ((num1 / num2) == b[index2]) || ((num1 / num2) == b[index3]))) {

        //disappearing ready statements
        t7.setVisibility(View.GONE);
        t8.setVisibility(View.GONE);

        //reappearing widgets
        t4.setVisibility(View.VISIBLE);
        t5.setVisibility(View.VISIBLE);
        t6.setVisibility(View.VISIBLE);

        b7.setVisibility(View.VISIBLE);
        b8.setVisibility(View.VISIBLE);
        b9.setVisibility(View.VISIBLE);

        tapbg.setOnClickListener(null); //disallowing tapbg to be clicked

        //--------------------------------------------------------------------------------------

        x = myrandom.nextInt(4); //0-3

        num1 = myrandom.nextInt(13); //0-12
        num2 = myrandom.nextInt(13); //0-12

        //randomizing indexes
        index1 = myrandom.nextInt(3); //0-2
        index2 = myrandom.nextInt(3); //0-2
        index3 = myrandom.nextInt(3); //0-2

        //making sure all 3 INDEXES are diff
        while (index1 == index2 || index2 == index3 || index1 == index3) {

            index1 = myrandom.nextInt(3); //0-2
            index2 = myrandom.nextInt(3); //0-2
            index3 = myrandom.nextInt(3); //0-2
        }


        if (x == 0) {
            ans = num1 + num2;

            y = myrandom.nextInt(25); //0-24
            z = myrandom.nextInt(25); //0-24

            //making sure all 3 ANSWERS are diff
            while (y == ans || y == z || z == ans) {
                y = myrandom.nextInt(25); //0-24
                z = myrandom.nextInt(25); //0-24
            }

            a[0] = ans;
            a[1] = y;
            a[2] = z;

            b7.setText(+ a[index1] +"");
            b8.setText(+ a[index2] +"");
            b9.setText(+ a[index3] +"");

            t6.setText(+ num1 + " + " + num2 + " = ?");
        }

        else if (x == 1) {
            ans = num1 - num2;

            y = (int)(-13 + (Math.random() * 26)); //-12 to 12
            z = (int)(-13 + (Math.random() * 26)); //-12 to 12

            //making sure all 3 ANSWERS are diff
            while (y == ans || y == z || z == ans) {
                y = (int)(-13 + (Math.random() * 26)); //-12 to 12
                z = (int)(-13 + (Math.random() * 26)); //-12 to 12
            }

            a[0] = ans;
            a[1] = y;
            a[2] = z;

            b7.setText(+ a[index1] +"");
            b8.setText(+ a[index2] +"");
            b9.setText(+ a[index3] +"");

            t6.setText(+num1 + " - " + num2 + " = ?");
        }

        else if (x == 2) {
            ans = num1 * num2;

            y = myrandom.nextInt(145); //0-144
            z = myrandom.nextInt(145); //0-144

            //making sure all 3 ANSWERS are diff
            while (y == ans || y == z || z == ans) {
                y = myrandom.nextInt(145); //0-144
                z = myrandom.nextInt(145); //0-144
            }

            a[0] = ans;
            a[1] = y;
            a[2] = z;

            b7.setText(+ a[index1] +"");
            b8.setText(+ a[index2] +"");
            b9.setText(+ a[index3] +"");

            t6.setText(+num1 + " x " + num2 + " = ?");
        }

        else if (x == 3) {
            //preventing num2 from being 0
            num2 = (int)(1 + (Math.random() * 11)); //1-12

            divideAns = num1 / num2;

            l = myrandom.nextInt(13); //0-12
            m = myrandom.nextInt(13); //0-12


            //Rounding up all the ANSWERS
            divideAnsRound = Math.round(divideAns * 100) / 100.0;

            i = Math.round(l * 100) / 100.0;
            j = Math.round(m * 100) / 100.0;


            //making sure all 3 ANSWERS are diff
            while (i == divideAnsRound || i == j || j == divideAnsRound) {
                l = myrandom.nextInt(13); //0-12
                m = myrandom.nextInt(13); //0-12

                i = Math.round(l * 100) / 100.0;
                j = Math.round(m * 100) / 100.0;
            }

            b[0] = divideAnsRound;
            b[1] = i;
            b[2] = j;

            b7.setText(+ b[index1] +"");
            b8.setText(+ b[index2] +"");
            b9.setText(+ b[index3] +"");

            t6.setText(+num1 + " ÷ " + num2 + " = ?");
        }

    }

    if ((v == b7 || v == b8 || v == b9) &&
            (((num1 + num2) != a[index1]) || ((num1 + num2) != a[index2]) || ((num1 + num2) != a[index3]) ||
             ((num1 - num2) != a[index1]) || ((num1 - num2) != a[index2]) || ((num1 - num2) != a[index3]) ||
             ((num1 * num2) != a[index1]) || ((num1 * num2) != a[index2]) || ((num1 * num2) != a[index3]) ||
             ((num1 / num2) != b[index1]) || ((num1 / num2) != b[index2]) || ((num1 / num2) != b[index3]))) {

        finish();
        startActivity(new Intent(Game.this, MainActivity.class));

    }

}