zoukankan      html  css  js  c++  java
  • 简单的四则运算

    首先画一个界面。

     <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="1">

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="TextView"
            android:layout_weight="0.06" />

        <Button
            android:id="@+idn1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.06"
            android:text="answer" />

        <Button
            android:id="@+idn2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.06"
            android:text="下一题" />
    </LinearLayout>
     
    然后定义变量

    public class MyActivity extends AppCompatActivity {
    private int a,b,c,d,rs;
    private TextView tv;
    private Button btn1,btn2;

       @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            tv = (TextView)findViewById(R.id.textView);
            btn1 = (Button)findViewById(R.id.btn1);
            btn2 = (Button)findViewById(R.id.btn2);

            rd();

    btn1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    answer(a,b,c,d);
    }
    });
    btn2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    show(a,b,c,d);
    }
    });
    }

    实现点击按钮显示答案的代码

    public void answer(int a,int b,int c,int d){
    switch (rs){
    case 0:
    tv.setText(a+"+"+b+"="+(a+b));
    break;
    case 1:
    tv.setText(a+"-"+b+"="+(a-b));
    break;
    case 2:
    tv.setText(a+"*"+b+"="+(a*b));
    break;
    case 3:
    tv.setText(a+"/"+b+"="+(a/b));
    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+"/"+c+"="+(a+b/c));
    break;
    case 7:
    tv.setText(a+"-"+b+"/"+c+"="+(a-b/c));
    break;
    case 8:
    tv.setText(a+"+"+b+"-"+c+"*"+d+"="+(a+b-c*d));
    break;
    case 9:
    tv.setText(a+"+"+b+"-"+c+"/"+d+"="+(a+b-c/d));
    break;
    case 10:
    tv.setText(a+"-"+b+"+"+c+"*"+d+"="+(a-b+c*d));
    break;
    case 11:
    tv.setText(a+"-"+b+"+"+c+"/"+d+"="+(a-b+c/d));
    break;
    case 12:
    tv.setText(a+"+"+b+"+"+c+"+"+d+"="+(a+b+c+d));
    break;
    case 13:
    tv.setText(a+"+"+b+"+"+c+"-"+d+"="+(a+b+c-d));
    break;
    case 14:
    tv.setText(a+"+"+b+"-"+c+"-"+d+"="+(a+b-c-d));
    break;
    case 15:
    tv.setText(a+"*"+b+"*"+c+"*"+d+"="+(a*b*c*d));
    break;
    case 16:
    tv.setText(a+"*"+b+"*"+c+"+"+d+"="+(a*b*c+d));
    break;
    case 17:
    tv.setText(a+"*"+b+"+"+c+"+"+d+"="+(a*b+c+d));
    break;
    case 18:
    tv.setText(a+"*"+b+"*"+c+"-"+d+"="+(a*b*c-d));
    break;
    case 19:
    tv.setText(a+"*"+b+"-"+c+"-"+d+"="+(a*b-c-d));
    break;

    }

    }

    实现显示题目的代码

    public void show(int a,int b,int c,int d){
    switch (rs){
    case 0:
    tv.setText(a+"+"+b+"="+"?");
    break;
    case 1:
    tv.setText(a+"-"+b+"="+"?");
    break;
    case 2:
    tv.setText(a+"*"+b+"="+"?");
    break;
    case 3:
    tv.setText(a+"/"+b+"="+"?");
    break;
    case 4:
    tv.setText(a+"+"+b+"*"+c+"="+"?");
    break;
    case 5:
    tv.setText(a+"-"+b+"*"+c+"="+"?");
    break;
    case 6:
    tv.setText(a+"+"+b+"/"+c+"="+"?");
    break;
    case 7:
    tv.setText(a+"-"+b+"/"+c+"="+"?");
    break;
    case 8:
    tv.setText(a+"+"+b+"-"+c+"*"+d+"="+"?");
    break;
    case 9:
    tv.setText(a+"+"+b+"-"+c+"/"+d+"="+"?");
    break;
    case 10:
    tv.setText(a+"-"+b+"+"+c+"*"+d+"="+"?");
    break;
    case 11:
    tv.setText(a+"-"+b+"+"+c+"/"+d+"="+"?");
    break;
    case 12:
    tv.setText(a+"+"+b+"+"+c+"+"+d+"="+"?");
    break;
    case 13:
    tv.setText(a+"+"+b+"+"+c+"-"+d+"="+"?");
    break;
    case 14:
    tv.setText(a+"+"+b+"-"+c+"-"+d+"="+"?");
    break;
    case 15:
    tv.setText(a+"*"+b+"*"+c+"*"+d+"="+"?");
    break;
    case 16:
    tv.setText(a+"*"+b+"*"+c+"+"+d+"="+"?");
    break;
    case 17:
    tv.setText(a+"*"+b+"+"+c+"+"+d+"="+"?");
    break;
    case 18:
    tv.setText(a+"*"+b+"*"+c+"-"+d+"="+"?");
    break;
    case 19:
    tv.setText(a+"*"+b+"-"+c+"-"+d+"="+"?");
    break;

    }
    }

    生成随机数字

    private void rd() {
    Random random = new Random();
    random.nextInt();
    a = random.nextInt(100);
    b = random.nextInt(100);
    c = random.nextInt(100);
    d = random.nextInt(100);
    rs = random.nextInt(20);
    }
    }

    由于android基础没打好,有很多不完善的地方,望见谅。

  • 相关阅读:
    [MacOS]Sublime text3 安装(一)
    [RHEL8]开启BBR
    PAT Advanced 1136 A Delayed Palindrome (20分)
    PAT Advanced 1144 The Missing Number (20分)
    PAT Advanced 1041 Be Unique (20分)
    PAT Advanced 1025 PAT Ranking (25分)
    PAT Advanced 1022 Digital Library (30分)
    PAT Advanced 1019 General Palindromic Number (20分)
    PAT Advanced 1011 World Cup Betting (20分)
    PAT Advanced 1102 Invert a Binary Tree (25分)
  • 原文地址:https://www.cnblogs.com/xukeyu/p/6545485.html
Copyright © 2011-2022 走看看