首页 > 代码库 > 简单的四则运算
简单的四则运算
这周的作业是四则运算,这个项目对于我来说可能有点困难,所以接下来的代码可能有些瑕疵。
java代码:
public class MainActivity extends siActivity {
private int a,b,c,ty;
private TextView tv;
private Button show,next;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.textView2);
show = (Button)findViewById(R.id.button3);
next = (Button)findViewById(R.id.button4);
sz();
switch (ty) {
case 0: tv.setText(a + "+" + b + "=" + "?");
break;
case 1: tv.setText(a + "-" + b + "=" + "?");
break;
case 2: tv.setText(a + "+" + b +"*"+ c + "=" + "?");
break;
case 3: tv.setText(a + "+" + b +"/"+ c + "=" + "?");
break;
case 4: tv.setText(a + "-" + b +"*"+ c + "=" + "?");
break;
case 5: tv.setText(a + "-" + b +"/"+ c + "=" + "?");
break;
case 6: tv.setText(a + "*" + b + "=" + "?");
break;
case 7: tv.setText(a + "/" + b + "=" + "?");
break;
}
show.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
switch (ty) {
case 0: tv.setText(a + "+" + b + "=" + (a + b));
break;
case 1: tv.setText(a + "-" + b + "=" + (a - b));
break;
case 2: tv.setText(a + "+" + b +"*"+ c + "="+ (a+b*c));
break;
case 3: tv.setText(a + "+" + b +"/"+ c + "="+(a+b/c));
break;
case 4: tv.setText(a + "-" + b +"*"+ c + "="+(a-b*c));
break;
case 5: tv.setText(a + "-" + b +"/"+ c + "="+(a-b/c));
break;
case 6: tv.setText(a + "*" + b + "=" + (a*b));
break;
case 7: tv.setText(a + "/" + b + "=" + (a/b));
break;
}
}
});
next.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
sz(); switch (ty) {
case 0: tv.setText(a + "+" + b + "=" + "?");
break;
case 1: tv.setText(a + "-" + b + "=" + "?");
break;
case 2: tv.setText(a + "+" + b +"*"+ c + "=" + "?");
break;
case 3: tv.setText(a + "+" + b +"/"+ c + "=" + "?");
break;
case 4: tv.setText(a + "-" + b +"*"+ c + "=" + "?");
break;
case 5: tv.setText(a + "-" + b +"/"+ c + "=" + "?");
break;
case 6: tv.setText(a + "*" + b + "=" + "?");
break;
case 7: tv.setText(a + "/" + b + "=" + "?");
break;
}
}
});
} private void sz(){
Random random = new Random();
random.nextInt();
a = random.nextInt(100);
b = random.nextInt(100);
c = random.nextInt(100);
ty = random.nextInt(8);
}
}
虚拟机运行
简单的四则运算
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。