首先完成生成随机数,包括随机生成几组数字,生成随机数的范围,四则运算符号等,要完成具体逻辑,进行加减乘除的分类,指定随机生成几组数字,然后再排列数字和四则运算符号。
这个四则运算还有很多不完善的地方,代码也不是最好的方法,敬请谅解。
下面是部分代码:
public class MainActivity extends AppCompatActivity { private int a,b,c,d,sy; private TextView textView; private Button btn_answer,btn_next; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView=(TextView)findViewById(R.id.textView); btn_answer = (Button)findViewById(R.id.btn_answer); btn_next = (Button)findViewById(R.id.btn_next); sz(); sw(); //点击显示按钮时显示答案 btn_answer.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { switch (sy) { case 0: textView.setText(a + "+" + b + "=" + (a + b)); break; case 1: textView.setText(a + "-" + b + "=" + (a - b)); break; case 2: textView.setText(a + "+" + b +"*"+ c + "="+ (a+b*c)); break; case 3: textView.setText(a + "+" + b +"/"+ c + "="+(a+b/c)); break; case 4: textView.setText(a + "-" + b +"*"+ c + "="+(a-b*c)); break; case 5: textView.setText(a + "-" + b +"/"+ c + "="+(a-b/c)); break; case 6: textView.setText(a + "*" + b + "=" + (a*b)); break; case 7: textView.setText(a + "/" + b + "=" + (a/b)); break; } } }); //点击下一题 btn_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); sy = random.nextInt(8); } //显示题目 private void sw(){ switch (sy) { case 0: textView.setText(a + "*" + b + "+" + c + "-" + d + "=?"); break; case 1: textView.setText(a + "*" + b + "/" + c + "+" + d + "=?"); break; case 2: textView.setText(a + "*" + b + "-" + c + "+" + d + "=?"); break; case 3: textView.setText(a + "/" + b + "*" + c + "+" + d + "=?"); break; } } }
运行截图: