java

时间:2015-06-08 16:47:24

标签: java android arrays if-statement

我正在尝试编写一些代码,其中有一个带有循环数字集(0-9)的textview,用户单击以匹配屏幕上显示的当前数字与数组中的下一个数字。如果在显示正确的数字时单击,则会在前一个旁边创建一个带有另一个循环的新textview,然后单击以匹配数组中的下一个数字,依此类推。如果您在屏幕上显示正确的号码时未能点击,则表示游戏结束。

我设法提出下面的代码,但它没有按照我的预期工作,我不知道为什么。目前,每次点击它都会直接进入游戏结束,如果我只使用其中一个'那么'除非我第一次点击错误,否则我永远无法进行游戏。有谁知道为什么会这样,以及我如何解决它?我的印象是把'ifs'和' elses'在另一个内部,如果意味着这些只适用于外部的“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");
    }
}

此外,如果有人知道更好的方法来检查针对阵列的循环整数集,那么请让我知道,我觉得我是以迂回的方式做这件事......

非常感谢。

0 个答案:

没有答案