-1

我正在做一个小测验(5 个问题),用户在一个问题上得到 1 到 5 分。每个问题有 5 个答案,所以 5 个按钮。

我不想要 5 种不同的活动,所以我考虑在用户单击随机答案/按钮时更改按钮中的文本视图 + 问题。每个按钮都有 1 到 5 个点,但始终是同一个按钮。因此,例如,如果他点击按钮 1 的第一个问题得到 1 分,但如果他点击按钮 4 的第二个问题,他得到 4 分。说得通?

所以我的问题是我的 if 语句不起作用,我不知道为什么。它可能真的很小,但这是我第一次为 android 编程,所以是的。:(

我的代码有什么问题?我没有任何错误。当我单击 button1 时,它变为“最后一个”并显示分数。

  public class Vraag1 extends Activity implements OnClickListener{

 Button a;
 Button b;
 Button c;
 Button d;
 Button e; 
 TextView scoreatm; 
 int int1 = 0;
 int int2 = 0;
 int int3 = 0;
 int int4 = 0;
 int int5 = 0;
 int intwelk = 0;


protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_vraag1);

        a = (Button) findViewById(R.id.button1);
        a.setOnClickListener(this);

        b = (Button) findViewById(R.id.button2);
        b.setOnClickListener(this);

        c = (Button) findViewById(R.id.button3);
        c.setOnClickListener(this);

        d = (Button) findViewById(R.id.button4);
        d.setOnClickListener(this);

        e = (Button) findViewById(R.id.button5);
        e.setOnClickListener(this);  

}

public void onClick(View v) {
// TODO Auto-generated method stub

      switch (v.getId()) {
         case R.id.button1: 

                if (intwelk == 0){
                    intwelk = 1;
                    int1 = 1; //1 point
                    a.setText("Change in this");        
                }

                if (intwelk == 1){  
                    intwelk = 2;
                    int2 = 1; //1 point
                    a.setText("Now this"); 
                }

                if (intwelk == 2){      
                    intwelk = 3;
                    int3 = 1; //1 point
                    a.setText("And this");      
                }

                if (intwelk == 3){          
                    intwelk = 4;
                    int4 = 1; //1 point
                    a.setText("Last one");          
                }

                if (intwelk == 4){
                    intwelk = 0;
                    int5 = 1; //1 point

                    showScore();
                }

          break;
         case R.id.button2:

                int1 = 2;

          break;
         case R.id.button3:

                int1 = 3;

          break;
         case R.id.button4:

                int1 = 4;

          break;
         case R.id.button5:

                int1 = 5;

          break;

      }
   }
4

1 回答 1

0

哇哦。刚发这个的时候我就明白了,太好了。

需要从 intwelk == 4 开始,然后是 intwelk == 3,然后是 intwelk == 2。等等。

有道理,因为代码是从上到下读取的。估计要晚了!

于 2013-11-14T17:08:21.013 回答