接受整数值的程序&计算剩余的天数

时间:2014-10-16 13:47:04

标签: java android

public class MainActivity extends Activity {

    TextView output;
    EditText month, day;
    Button compute;

    @Override
    protected void onCreate(Bundle b) {
        super.onCreate(b);
        setContentView(R.layout.activity_main);

        day = (EditText) findViewById(R.id.editText1);
        month = (EditText) findViewById(R.id.editText2);
        output = (TextView) findViewById(R.id.textView3);
        compute = (Button) findViewById(R.id.button1);

        compute.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                int x = 0, y = 0, result = 0;

                x = Integer.parseInt(month.getText().toString());
                y = Integer.parseInt(day.getText().toString());

                if (x == 1 && y <= 31) {
                    result = 359 - y;
                    output.setText(result + "Days Before Christmass");
                } else if (x == 2 && y <= 28) {
                    result = 328 - y;
                    output.setText(result + "Days Before Christmass");
                } else if (x == 3 && y <= 31) {
                    result = 297 - y;
                    output.setText(result + "Days Before Christmass");
                } else if (x == 4 && y <= 30) {
                    result = 267 - y;
                    output.setText(result + "Days Before Christmass");
                } else if (x == 5 && y <= 31) {
                    result = 236 - y;
                    output.setText(result + "Days Before Christmass");
                } else if (x == 6 && y <= 30) {
                    result = 206 - y;
                    output.setText(result + "Days Before Christmass");
                } else if (x == 7 && y <= 31) {
                    result = 175 - y;
                    output.setText(result + "Days Before Christmass");
                } else if (x == 8 && y <= 31) {
                    result = 144 - y;
                    output.setText(result + "Days Before Christmass");
                } else if (x == 9 && y <= 30) {
                    result = 114 - y;
                    output.setText(result + "Days Before Christmass");
                } else if (x == 10 && y <= 31) {
                    result = 83 - y;
                    output.setText(result + "Days Before Christmass");
                } else if (x == 11 && y <= 30) {
                    result = 53 - y;
                    output.setText(result + "Days Before Christmass");
                } else if (x == 12 && y <= 31) {
                    result = 22 - y;
                    output.setText(result + "Days Before Christmass");
                }

            }
        });
    }

}

这是我之前关于一个接受整数值&amp;的android程序的问题的修改代码。计算剩下的几天我现在输出错误而不是得到2-359 = 357我得到了圣诞节前327天的回答..非常感谢你的帮助

2 个答案:

答案 0 :(得分:0)

所以你得到1/2而不是2/1的答案。检查月份和日期是否输入到正确的插槽中(System.out.print()是您的朋友)。

另外,你错过了圣诞节。

答案 1 :(得分:0)

因此,在第1个月和第2天看到您的问题时,您会得到错误的结果。

所以通过你的代码,如果x == 1和y == 2,输出确实是357。但是,如果您错过了您的edittext,结果将是327,即x == 2和y == 1。这就是你得到的。所以我应该改变

 day = (EditText) findViewById(R.id.editText1);
 month = (EditText) findViewById(R.id.editText2);

 day = (EditText) findViewById(R.id.editText2);
 month = (EditText) findViewById(R.id.editText1);