生成四则运算

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了生成四则运算相关的知识,希望对你有一定的参考价值。

这周的任务是实现简单的四则运算,包括完成生成随机数,包括随机生成几组数字,生成随机数的范围,加减乘除四则运算符号。由于会的东西不怎么多,所以用的都是从网上学到的简单的代码。

界面:

技术分享

 

代码:

public class MainActivity extends MyActivity {
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();
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;
}
}
});
//点击下一题
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;
}
}
}public class MainActivity extends MyActivity {
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();
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;
}
}
});
//点击下一题
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;
}
}
}

 虚拟机运算结果:

技术分享技术分享

 

























































































































































































以上是关于生成四则运算的主要内容,如果未能解决你的问题,请参考以下文章

结对-四则运算生成器-设计文档

自动生成四则运算题目

生成四则运算

四则运算题目生成(python版)

个人作业1——四则运算题目生成程序

个人项目:自动生成四则运算题目总结