• 简单的四则运算


    本周的作业是用Android写一个简单的四则运算,其中运算功能我实现的差不多了,我用的是很蠢的方法——把运算逻辑一个个列出来。在除法运算的时候因为我定义的随机数是整数所以无法完成分数的运算。总的来说我这个四则运算还有很多不完善的地方,代码很繁琐也不是最好的方法,看到其他人的代码我觉得差距还是很大的。

    下面是代码部分:

    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.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;
    }
    }
    }
    界面部分:

    运算结果:

    
    
    
  • 相关阅读:
    4.方向-世界坐标系
    3.Unity3d物体操作常用按键
    2.Unity3d常用按键
    1.Unity3d的新建场景和保存场景
    MySQL CodeFirst的配置与注意事项
    HNU暑假训练第一场C.Ninja Map
    牛客暑假多校第二场J-farm
    牛客暑假多校第一场J-Different Integers
    主席树设计与实现
    博弈论的一些理解
  • 原文地址:https://www.cnblogs.com/lw0607/p/6539291.html
Copyright © 2020-2023  润新知