2014-10-16 102 views
-3
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"); 
       } 

      } 
     }); 
    } 

} 

这里是我以前关于接受整数值&计算剩下的日子现在我让我的输出错误的机器人程序的问题我修改后的代码而不是得到2-359 = 357我得到圣诞节前327天的答案..你的帮助非常感谢程序,接受一个整数值与计算剩余天

+0

仅供参考'2-359 = -357'而不是357 – 2014-10-16 13:54:25

+0

你有什么投入?我假设x是1,y是什么? – thegrinner 2014-10-16 13:57:10

+0

@thegrinner y是一天,x是月......这使得如果(x == 1 && y <= 31)x是数字形式的月份并且y是天数 – jesse 2014-10-16 14:00:15

回答

0

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

另外,你错过了圣诞节。

+0

will do ..对不起, – jesse 2014-10-16 14:04:30

+0

感谢兄弟错误是在这部分代码日=(EditText)findViewById(R.id.editText1); month =(EditText)findViewById(R.id.editText2);它是差异。从我的xml文件 – jesse 2014-10-16 14:08:02

0

因此看到你的问题,你告诉第一个月和第二天你得到了错误的结果。

所以通过你的代码,如果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); 
+0

谢谢,我发现它。 – jesse 2014-10-16 14:09:13