应用程序运行时无需任何用户交

时间:2014-03-31 02:42:00

标签: java android android-button

显示两个随机数并计算奇数和偶数转数的分数。当其中一个达到100时,应用程序停止。

这是我的代码..

public class Board_Play1 extends Activity {

int d=0,a=0,b=0,turn=2;
Random random = new Random();

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.board_play1);
    EditText diceno = (EditText) findViewById(R.id.editText1);
    EditText p1 = (EditText) findViewById(R.id.editText2);
    EditText p2 = (EditText) findViewById(R.id.editText3);

    diceno.setText(String.valueOf(d));  
    p1.setText(String.valueOf(a));
    p2.setText(String.valueOf(b));

    while(a!=100 && b!=100)
    {
        if(turn%2==0)
        {
            Button button = (Button) findViewById(R.id.button1);
            button.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View view) {
                    // TODO Auto-generated method stub
                    d=random.nextInt(6)+1;
                    EditText diceno = (EditText) findViewById(R.id.editText1);
                    diceno.setText(String.valueOf(d));

                }
            }); 
        }
        else
        {

            d=random.nextInt(6)+1;
            diceno.setText(String.valueOf(d));
        }

    if(turn%2==0)
            a+=d;
        else
            b+=d;

    if(a>100)
        a-=d;
    if(b>100)
        b-=d;


        p1.setText(String.valueOf(a));
        p2.setText(String.valueOf(b));
        turn++;
    }
    a=0;b=0;

}


}

当我跑这个......它就像......一切都完成了。始终p2 editText获得100而p1将有一些小于100且diceno的值为100-p1

但这不是我想问的主要问题。我添加了Buttons和onClickListener方法,只有当用户点击该特定按钮时才会运行。但是没有发生这样的事情。一旦我打开应用程序,它就会获得最大值。价值和用户无关。

为什么会那样?帮助我做什么,以便从0p1中的值p2开始。

1 个答案:

答案 0 :(得分:1)

试试这个,我改变了你的代码

public class Board_Play1 extends Activity {

int d=0,a=0,b=0,turn=2;
Random random = new Random();

@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.board_play1);
EditText diceno = (EditText) findViewById(R.id.editText1);
EditText p1 = (EditText) findViewById(R.id.editText2);
EditText p2 = (EditText) findViewById(R.id.editText3);

diceno.setText(String.valueOf(d));  
p1.setText(String.valueOf(a));
p2.setText(String.valueOf(b));
EditText diceno = (EditText) findViewById(R.id.editText1);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                // TODO Auto-generated method stub
                d=random.nextInt(6)+1;
               diceno.setText(String.valueOf(d));

                if(turn%2==0)
                        a+=d;
                    else
                        b+=d;

                if(a>100)
                    a-=d;
                if(b>100)
                    b-=d;


                    p1.setText(String.valueOf(a));
                    p2.setText(String.valueOf(b));
                    turn++;
            }


        }); 
 }


}

更新:我将再添加一个方法来计算Digeno。我认为这会对你有所帮助

public class Board_Play1 extends Activity {

int d=0,a=0,b=0,turn=2;
Random random = new Random();

@Override
protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.board_play1);
        EditText diceno = (EditText) findViewById(R.id.editText1);
        EditText p1 = (EditText) findViewById(R.id.editText2);
        EditText p2 = (EditText) findViewById(R.id.editText3);

        diceno.setText(String.valueOf(d));  
        p1.setText(String.valueOf(a));
        p2.setText(String.valueOf(b));
        EditText diceno = (EditText) findViewById(R.id.editText1);
        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        // This is user Turn
                        d=random.nextInt(6)+1;
                        diceno.setText(String.valueOf(d));
                        calculateDiceno();

                        //This is Application Trun. You may add some delay with progress bar

                        d=random.nextInt(6)+1;
                        diceno.setText(String.valueOf(d));
                        calculateDiceno();


                    }

        }); 


 }

    private void calculateDiceno(){
           if(turn%2==0)
                a+=d;
            else
                b+=d;

        if(a>100)
            a-=d;
        if(b>100)
            b-=d;


            p1.setText(String.valueOf(a));
            p2.setText(String.valueOf(b));
            turn++;
        }


}

更新2:不要使用sleep()。使用处理程序有一些延迟。

//This is Application Trun. You may add some delay with progress bar
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
          //This will execute after 3 sec
          d=random.nextInt(6)+1;
      diceno.setText(String.valueOf(d));
      calculateDiceno();
    }
}, 3000);  // 3000 means set 3 sec delay.