java中嵌套if语句的意外结果

java中嵌套if语句的意外结果,java,android,arrays,if-statement,Java,Android,Arrays,If Statement,我试图编写一些代码,其中有一个文本视图,其中有一组循环的数字(0-9),用户单击以将屏幕上显示的当前数字与数组中的下一个数字匹配。如果在显示正确的数字时单击,则会在上一个循环的旁边创建一个带有另一个循环的新textview,您可以单击以匹配数组中的下一个数字,依此类推。如果你在屏幕上显示正确的数字时没有点击,游戏就结束了 我设法想出了下面的代码,但它没有按照我的预期工作,我不知道为什么。目前,每次点击都会直接进入游戏结束,如果我只使用一个“其他”我永远无法让它进入游戏结束,除非我第一次点击错误。

我试图编写一些代码,其中有一个文本视图,其中有一组循环的数字(0-9),用户单击以将屏幕上显示的当前数字与数组中的下一个数字匹配。如果在显示正确的数字时单击,则会在上一个循环的旁边创建一个带有另一个循环的新textview,您可以单击以匹配数组中的下一个数字,依此类推。如果你在屏幕上显示正确的数字时没有点击,游戏就结束了

我设法想出了下面的代码,但它没有按照我的预期工作,我不知道为什么。目前,每次点击都会直接进入游戏结束,如果我只使用一个“其他”我永远无法让它进入游戏结束,除非我第一次点击错误。有人知道为什么会发生这种情况,以及我如何解决它吗?我的印象是,将“ifs”和“ELSE”放在另一个if中意味着只有在满足外部“ifs”条件的情况下,这些才适用。我有点不知所措,因为我想检查50+整数,所以如果它不能与2一起工作,那么我注定要失败:(

相关代码:

public class MainActivity extends Activity {

private int a = 0;
private int b = 0;
TextView textView;
TextView2 textView2;
LinearLayout container;
RelativeLayout relativeLayout;
private Handler handler = new Handler();

public Runnable myRunnable = new Runnable() {
    @Override
    public void run() {
        updateText;
        a = ++a % 10;
        handler.postDelayed(this, 1000)
    }
};

public Runnable myRun2 = new Runnable() {
    @Override
    public void run() {
        updateText2;
        b = ++b % 10;
        handler.postDelayed(this, 900)
    }
};

public int aNums[] = { 1, 4, 8, 5, 0, 2, 7, 8, 6... };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = (TextView) findViewById(R.id.textView);
    loop();
    relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
    relativeLayout.setOnClickListener(handler2);
    container = (LinearLayout) findViewById(R.id.linearLayout);
}

View.OnClickListener handler2 = new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        textView2 = new TextView(getApplicationContext());
        textView2.setTextAppearance(getApplicationContext(), R.style.textViewStyle);
        textView2.setOnClickListener(this);
        if (a == aNums[0]) {
            stop();
            loop2();
            container = (LinearLayout) findViewById(R.id.linearLayout);
            container.addView(textView2);
            if (b == aNums[1]) {
                textView3 = new TextView(getApplicationContext());
                textView3.setTextAppearance(getApplicationContext(), R.style.textViewStyle);
                textView3.setOnClickListener(this);
                stop2();
                loop3();
                container.addView(textView3);
                // Ideally would add more if statements in the same way from here e.g. if (c == aNums[2]) { etc.
            }
            else { //<--If I take this else out, I can't get game over.
                gameOver(); 
            }
        }
        else {
            gameOver();
        }
    }
};

// Relevant methods:
    public void stop() {
        super.onStop();
        handler.removeCallbacks(myRunnable);
    }

    public void stop2() {
        super.onStop();
        handler.removeCallbacks(myRun2);
    }

    public void loop() {
        handler.post(myRunnable);
    }

    public void loop2() {
        handler.post(myRun2);
    }

    public void loop3() {
        handler.post(myRun3);
    }

    public void updateText() {
        textView.setText("" + a);
    }

    public void updateText2() {
        textView2.setText("" + b);
    }

    public void gameOver() {
        textView.setText("Game Over");
    }
}
公共类MainActivity扩展活动{
私有INTA=0;
私有int b=0;
文本视图文本视图;
TextView2 TextView2;
线性布局集装箱;
相对的相对的;
私有处理程序=新处理程序();
public Runnable myRunnable=new Runnable(){
@凌驾
公开募捐{
updateText;
a=++a%10;
handler.postDelayed(这个,1000)
}
};
public Runnable myRun2=new Runnable(){
@凌驾
公开募捐{
updateText2;
b=++b%10;
handler.postDelayed(这个,900)
}
};
公共国际[]={1,4,8,5,0,2,7,8,6…};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(textView)findViewById(R.id.textView);
loop();
relativeLayout=(relativeLayout)findViewById(R.id.relativeLayout);
relativeLayout.setOnClickListener(handler2);
容器=(LinearLayout)findViewById(R.id.LinearLayout);
}
View.OnClickListener handler2=新视图。OnClickListener(){
@凌驾
公共void onClick(视图v){
textView2=新的TextView(getApplicationContext());
textView2.setTextAppearance(getApplicationContext(),R.style.textViewStyle);
textView2.setOnClickListener(此);
如果(a==aNums[0]){
停止();
loop2();
容器=(LinearLayout)findViewById(R.id.LinearLayout);
container.addView(textView2);
if(b==aNums[1]){
textView3=新的TextView(getApplicationContext());
textView3.setTextAppearance(getApplicationContext(),R.style.textViewStyle);
textView3.setOnClickListener(此);
stop2();
loop3();
container.addView(textView3);
//理想情况下,可以从这里以相同的方式添加更多的if语句,例如if(c==aNums[2]){等。
}

否则{//在执行条件之前,您能打印“a”和“b”的值吗?这可能与您预期的不同。@Jimmy不确定您的意思,但a和b只是我给它们的标记,因此我可以分别引用stop()、loop()和updateText()方法。这两个方法都将始终是从0到9的循环集。我的意思是,您的aNums[]似乎是常量。根据您的条件,gameover不仅在a为1且b为4时调用。这是在假设您的aNums[]在初始化后未被修改的情况下进行的。因此,我只希望您打印变量和aNums[x]的值在条件之前。@Jimmy Oh好的,当我打印条件a和b之前的值时,它们都是0。如果我一次单击打印它们,a是当时屏幕上显示的任何数字,b是0。但是你的aNums[]是常数对吗?如果是这样,为了不结束游戏,你需要a=1和b=4,否则,你的流程将转到gameover。尝试调试,我想你的实际情况和期望是不同的。