简单四则运算
实现一个简单的四则混合运算。并且在计算100范围以内。我实现的是两位数之间的加减乘除混合运算,首先先定义几个量。
然后思考一下你所想要的计算方式,我在这里就简单的进行了数字的加减乘除,进行分为各种情况,这里仅仅把四组的数字的混合运算一下,然后添加监听并达到的要求。
我的这个四则运算存在很多的不足,例如乘除法的优先级问题,是个比较大的bug,还有分数不能进行等。也不能随机生成不同位数的数字进行混合运算。代码繁琐也不好达到预期效果。
生成四组随机数,四种组合方式。采用switch case方法,并且进行题目的展示出来。
虚拟机进行运行,并显示出结果。
总体而言,该次任务存在很多的不足,需要加以改正,存在许多的bug,希望以后能够继续完善,完成各种情况。以下是上面描述的所有代码。
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
//定义变量
private int sy;
private int a;
private int b;
private int c;
private int d;
private TextView tv;
private Button btn_answers;
private Button btn_next;
//实现监听按钮功能
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
btn_answers = (Button) findViewById(R.id.btn_answers);
btn_next = (Button) findViewById(R.id.btn_next);
suiji();
xianshi();
btn_answers.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (sy) {
case 0:
tv.setText(a + "*" + b + "+" + c + "-" + d + "=?");
break;
case 1:
tv.setText(a + "*" + b + "/" + c + "+" + d + "=?");
break;
case 2:
tv.setText(a + "*" + b + "-" + c + "+" + d + "=?");
break;
case 3:
tv.setText(a + "/" + b + "*" + c + "+" + d + "=?");
break;
}
}
});
btn_next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
suiji();
xianshi();
}
});
}
//随机数生成
private void suiji() {
a = new Random().nextInt(100);
b = new Random().nextInt(100);
c = new Random().nextInt(100);
d = new Random().nextInt(100);
sy = new Random().nextInt(4);
}
//显示出题目
private void xianshi() {
switch (sy) {
case 0:
tv.setText(a + "*" + b + "+" + c + "-" + d + "=" + (a * b + c - d));
break;
case 1:
tv.setText(a + "*" + b + "/" + c + "+" + d + "=" + (a * b / c + d));
break;
case 2:
tv.setText(a + "*" + b + "-" + c + "+" + d + "=" + (a * b - c + d));
break;
case 3:
tv.setText(a + "/" + b + "*" + c + "+" + d + "=" + (a / b * c + d));
break;
}
}
}
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
//定义变量
private int sy;
private int a;
private int b;
private int c;
private int d;
private TextView tv;
private Button btn_answers;
private Button btn_next;
//实现监听按钮功能
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
btn_answers = (Button) findViewById(R.id.btn_answers);
btn_next = (Button) findViewById(R.id.btn_next);
suiji();
xianshi();
btn_answers.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (sy) {
case 0:
tv.setText(a + "*" + b + "+" + c + "-" + d + "=?");
break;
case 1:
tv.setText(a + "*" + b + "/" + c + "+" + d + "=?");
break;
case 2:
tv.setText(a + "*" + b + "-" + c + "+" + d + "=?");
break;
case 3:
tv.setText(a + "/" + b + "*" + c + "+" + d + "=?");
break;
}
}
});
btn_next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
suiji();
xianshi();
}
});
}
//随机数生成
private void suiji() {
a = new Random().nextInt(100);
b = new Random().nextInt(100);
c = new Random().nextInt(100);
d = new Random().nextInt(100);
sy = new Random().nextInt(4);
}
//显示出题目
private void xianshi() {
switch (sy) {
case 0:
tv.setText(a + "*" + b + "+" + c + "-" + d + "=" + (a * b + c - d));
break;
case 1:
tv.setText(a + "*" + b + "/" + c + "+" + d + "=" + (a * b / c + d));
break;
case 2:
tv.setText(a + "*" + b + "-" + c + "+" + d + "=" + (a * b - c + d));
break;
case 3:
tv.setText(a + "/" + b + "*" + c + "+" + d + "=" + (a / b * c + d));
break;
}
}
}