要须心地收汗马,孔孟行世目杲杲。这篇文章主要讲述简单的Android四则运算相关的知识,希望能为你提供帮助。
布局界面:
文章图片
布局界面的代码:
文章图片
java代码:
点击按钮答案时显示答案:
public class MainActivity extends AppCompatActivity {
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.button1);
next = (Button)findViewById(R.id.button2);
sz();
sw();
//点击答案按钮时显示答案
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;
}
}
});
【简单的Android四则运算】点击显示下一题:
//点击下题
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sz();
sw();
}
});
}
生成随机数:
//生成随机数
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);
}
显示题目:
//显示题目
private void sw(){
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;
}
}
}
推荐阅读
- android 四则运算
- 有瑕疵的Android四则运算
- Frameworks detected: Android framework is detected in the project
- Android 6.0版本以后运行时权限提醒
- Android的学习总结
- android 编译打包烧录
- Android 软键盘控制方法以及开发中遇到的一些问题。转
- Android 系统中,那些能大幅提高工作效率的 API 汇总(持续更新中...)
- 浅谈Android保护技术__代码混淆