• 三位数的四则运算


    先上图:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:weightSum="1">

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

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <Button
    android:id="@+id/button4"
    android:layout_width="193dp"
    android:layout_height="wrap_content"
    android:text="显示" />

    <Button
    android:id="@+id/button3"
    android:layout_width="187dp"
    android:layout_height="wrap_content"
    android:text="下一题"
    android:layout_weight="0.01" />
    </LinearLayout>
    </LinearLayout>

    看到上面的图,大家肯定会说,这不算错了么,其实这是四舍五入的结果,

    代码逻辑,为:

    1. random生成随机数,包括数字a,b,c
    2. 加减乘除的实现:以switch列出所有的加减乘除的组合,三位数的算数(包括2位数)总共20种,列出所有可能。
    3. 显示答案:在同一个random下,例如显示a+b+c=?,然后在另一个方法中显示a+b+c=d。在点击下一题时,再次生成随机数,以此类推。

    代码如下:

    package com.example.myapplication;


    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;

    import java.util.Random;

    public class MainActivity extends AppCompatActivity {
    private int a, b, c, ysf;
    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.tv);
    show = (Button) findViewById(R.id.show);
    next = (Button) findViewById(R.id.next);
    rd();

    show.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    answer(ysf,a,b,c);
    }
    });
    next.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    rd();
    show(ysf,a,b,c);
    }
    });
    }
    private void answer(int ty,int a,int b,int c){
    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+"="+(a*b));
    break;
    case 3:
    tv.setText(a+"/"+b+"="+(a/b));
    break;
    //三个数的四则运算,共16种
    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+"="+(a-b-c));
    break;
    case 9:
    tv.setText(a+"-"+b+"+"+c+"="+(a-b+c));
    break;
    case 10:
    tv.setText(a+"-"+b+"*"+c+"="+(a-b*c));
    break;
    case 11:
    tv.setText(a+"-"+b+"/"+c+"="+(a-b/c));
    break;

    case 12:
    tv.setText(a+"*"+b+"*"+c+"="+(a*b*c));
    break;
    case 13:
    tv.setText(a+"*"+b+"+"+c+"="+(a*b+c));
    break;
    case 14:
    tv.setText(a+"*"+b+"-"+c+"="+(a*b-c));
    break;
    case 15:
    tv.setText(a+"*"+b+"/"+c+"="+(a*b/c));
    break;

    case 16:
    tv.setText(a+"/"+b+"/"+c+"="+(a/b/c));
    break;
    case 17:
    tv.setText(a+"/"+b+"+"+c+"="+(a/b+c));
    break;
    case 18:
    tv.setText(a+"/"+b+"-"+c+"="+(a/b-c));
    break;
    case 19:
    tv.setText(a+"/"+b+"*"+c+"="+(a/b*c));
    }
    }
    private void show(int ty,int a,int b,int c){
    switch (ty) {
    //将所有的可能全列出来
    //两个数的算法
    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;
    //三个数的四则运算,共16种
    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+"="+"?");
    break;
    case 9:
    tv.setText(a+"-"+b+"+"+c+"="+"?");
    break;
    case 10:
    tv.setText(a+"-"+b+"*"+c+"="+"?");
    break;
    case 11:
    tv.setText(a+"-"+b+"/"+c+"="+"?");
    break;

    case 12:
    tv.setText(a+"*"+b+"*"+c+"="+"?");
    break;
    case 13:
    tv.setText(a+"*"+b+"+"+c+"="+"?");
    break;
    case 14:
    tv.setText(a+"*"+b+"-"+c+"="+"?");
    break;
    case 15:
    tv.setText(a+"*"+b+"/"+c+"="+"?");
    break;

    case 16:
    tv.setText(a+"/"+b+"/"+c+"="+"?");
    break;
    case 17:
    tv.setText(a+"/"+b+"+"+c+"="+"?");
    break;
    case 18:
    tv.setText(a+"/"+b+"-"+c+"="+"?");
    break;
    case 19:
    tv.setText(a+"/"+b+"*"+c+"="+"?");
    }
    }

    private void rd() {
    Random random = new Random();
    random.nextInt();
    a = random.nextInt(100);
    b = random.nextInt(100);
    c = random.nextInt(100);
    ysf = random.nextInt(19);
    }
    }
  • 相关阅读:
    python:使用 watchdog 监控文件变化
    CSS 宽高度百分比加减像素的办法
    foxmail登陆失败LOGIN Login error user suspended
    IDEA 自动删除类中无用的import包
    CSS命名规范——BEM思想
    jQuery文件上传插件 uploadify 的使用
    textarea 固定大小,滚动条,限制拖动,文字对齐等
    命令行工具 Cmder 的使用与配置
    转:【MySQL】连接错误过多:is blocked because of many connection errors; unblock with 'mysqladmin flush-host
    OpenJFX的相关Maven依赖
  • 原文地址:https://www.cnblogs.com/firefoxman/p/6541451.html
Copyright © 2020-2023  润新知