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天的答案..你的帮助非常感谢程序,接受一个整数值与计算剩余天
仅供参考'2-359 = -357'而不是357 – 2014-10-16 13:54:25
你有什么投入?我假设x是1,y是什么? – thegrinner 2014-10-16 13:57:10
@thegrinner y是一天,x是月......这使得如果(x == 1 && y <= 31)x是数字形式的月份并且y是天数 – jesse 2014-10-16 14:00:15