每次我分开,我得0.0

时间:2015-08-20 12:41:05

标签: java android

我正在尝试创建一个可以在人与人之间分配账单的应用程序,但每当我尝试将代码中的两个数字分开时,它总是给我0.0。这是为什么?

以下是代码:

public class MainActivity extends Activity {
    public int x = 0;
    public double y = 1.00;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Button
        Button btn = (Button) findViewById(R.id.button);

        //EditText
        EditText nop = (EditText) findViewById(R.id.editText);
        EditText cob = (EditText) findViewById(R.id.editText2);


        try{
            x = Integer.valueOf(nop.getText().toString());
        } catch (NumberFormatException e) {
            x = 0;
        }

        try{
            y = Double.valueOf(cob.getText().toString());
        } catch (NumberFormatException e) {
            y = 1.00;
        }


        //TextView
        final TextView tv = (TextView) findViewById(R.id.textView);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                 Double z = (y / x);
                tv.setText("You will each pay:" + (z));
            }
        });

    }
    }

4 个答案:

答案 0 :(得分:4)

我的猜测是,您不会在点击后评估值。 try / catch的代码应该在onclick方法中。

答案 1 :(得分:0)

点击按钮从EditText获取值,否则您将无法获取输入的值,因为获取xy的值的代码会被执行一旦启动应用程序,就不再进行进一步的提取。

这样做

    final TextView tv = (TextView) findViewById(R.id.textView);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try{
                x = Integer.valueOf(nop.getText().toString());
            } catch (NumberFormatException e) {
                x = 0;
            }

            try{
                y = Double.valueOf(cob.getText().toString());
            } catch (NumberFormatException e) {
                y = 1.00;
            }

             Double z = (y / x);
            tv.setText("You will each pay:" + (z));
        }
    });

答案 2 :(得分:0)

如果以下代码中存在异常,则x将为零。

try {
    x = Integer.valueOf(nop.getText().toString());
} catch (NumberFormatException e) {
    x = 0;
}

现在因为例外,你除以零&将获得Infinity而不是0.0

答案 3 :(得分:0)

还要确保参与者的默认数量为1而不是零。如果将其设置为零,则最好不要进行除法并强制用户输入正确的数字,或者必须将除法的异常除以零。

public class MainActivity extends Activity {
    public int x = 1;
    public double y = 1.00;
    public EditText nop;
    public EditText cob;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Button
        Button btn = (Button) findViewById(R.id.button);

        //EditText
        nop = (EditText) findViewById(R.id.editText);
        cob = (EditText) findViewById(R.id.editText2);

        //TextView
        final TextView tv = (TextView) findViewById(R.id.textView);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try{
                    x = Integer.valueOf(nop.getText().toString());
                } catch (NumberFormatException e) {
                    x = 1;
                }

                try{
                    y = Double.valueOf(cob.getText().toString());
                } catch (NumberFormatException e) {
                    y = 1.00;
                }

                Double z = (y / x);
                tv.setText("You will each pay:" + (z));
            }
        });

    }
}