使用Do While循环获取输入,直到用户获得1

时间:2022-10-11 21:23:17

I want multiple input until user can't get 1. This will not work properly. In this case, can I take multiple edit text box, or not? Please tell me.

我想要多个输入,直到用户无法获得1.这将无法正常工作。在这种情况下,我可以采取多个编辑文本框吗?请告诉我。

TextView tv;
EditText et;
Button b;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.evenodd2);

    tv= (TextView) findViewById(R.id.textViewevenoddA);
    et= (EditText) findViewById(R.id.editTextevenodd);
    b= (Button) findViewById(R.id.buttonevenoddA);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String a1;
            int num;
            do
            {
                a1=et.getText().toString();
                num=Integer.parseInt(a1);
                if (num < 1)
                    tv.setText("Wrong Input");
                if (num > 1 && num % 2 == 0)
                {
                    tv.setText("Value is: "+num / 2);
                }
                else
                {
                    tv.setText("Value is :"+num);
                }
            }
            while (num != 1);
            tv.setText("Thank You");

        }
    });
}}

2 个解决方案

#1


1  

You wouldn't use a loop here. You'd do the processing exactly once every time the user presses the button. A loop getting input is how a console would work, but its not how a GUI driven program reacts.

你不会在这里使用循环。每次用户按下按钮时,您只需处理一次。获取输入的循环是控制台的工作方式,但不是GUI驱动程序的反应方式。

#2


0  

You need to give control back to the swing thread, so the user has a chance to change the input. This loop goes continuously, you need to return from the onClick() so the Event Dispatch Thread has a chance to wait for the user to change the value and click the button again.

您需要将控制权交还给回转线程,以便用户有机会更改输入。此循环连续进行,您需要从onClick()返回,以便Event Dispatch Thread有机会等待用户更改值并再次单击该按钮。

#1


1  

You wouldn't use a loop here. You'd do the processing exactly once every time the user presses the button. A loop getting input is how a console would work, but its not how a GUI driven program reacts.

你不会在这里使用循环。每次用户按下按钮时,您只需处理一次。获取输入的循环是控制台的工作方式,但不是GUI驱动程序的反应方式。

#2


0  

You need to give control back to the swing thread, so the user has a chance to change the input. This loop goes continuously, you need to return from the onClick() so the Event Dispatch Thread has a chance to wait for the user to change the value and click the button again.

您需要将控制权交还给回转线程,以便用户有机会更改输入。此循环连续进行,您需要从onClick()返回,以便Event Dispatch Thread有机会等待用户更改值并再次单击该按钮。