温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android随机给出加减乘除的四则运算方法

发布时间:2022-04-24 10:40:26 来源:亿速云 阅读:407 作者:zzz 栏目:开发技术

这篇文章主要介绍“Android随机给出加减乘除的四则运算方法”,在日常操作中,相信很多人在Android随机给出加减乘除的四则运算方法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Android随机给出加减乘除的四则运算方法”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >
   <TextView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="自动出题APP"
       android:textSize="35sp"
       android:textColor="#FF5722"
       android:gravity="center"
      android:layout_marginTop="20dp"
       />
    <TextView
android:layout_marginTop="100dp"
        android:id="@+id/tv_math"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:textSize="40sp"
        android:gravity="center_horizontal"
 
        />
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#E91E63"
        />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
 
    android:orientation="horizontal">
    <Button
        android:layout_marginTop="20dp"
        android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示题目"
        android:textSize="30sp"
        android:id="@+id/btn_question"
        />
    <Button
        android:background="#05FC2E"
        android:layout_marginLeft="100dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示答案"
        android:id="@+id/btn_answers"
        android:textSize="30sp"
        android:layout_marginTop="20dp"
 
        />
</LinearLayout>
</LinearLayout>

Android随机给出加减乘除的四则运算方法

Java代码文件

package com.example.dysuanshuti;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tv_math;
    private Button btn_question;
    private Button btn_answers;
   @ Override
    protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main) ;
    initView();
}
    private void initView(){
        tv_math = (TextView) findViewById(R.id.tv_math) ;
        btn_question = (Button) findViewById(R.id.btn_question);
        btn_answers = (Button) findViewById(R.id.btn_answers);
        btn_question.setOnClickListener(MainActivity.this );
        btn_answers.setOnClickListener(MainActivity.this ) ;
    }
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_question:
                question();
                break;
        }
   }
    private void question() {
 
        java.util.Random random = new java.util.Random();
 
//先指定随机生成几组数字(2~4);
  int temp = random.nextInt(3) + 2;
 
//随机生成(0~3)四个数字分别对应:+,-,×,÷
//如果随机到四组数字,则会有三个运算符
   int signal = random.nextInt(4);//第一个运算符
   int signal1 = random.nextInt(4);//第二个运算符
   int signal2 = random.nextInt(4);//第三个运算符
 
//指随机生成的每组数字的数值(1~11)
   final int num1 = random.nextInt(11) + 1;
   final int num2 = random.nextInt(11) + 1;
   final int num3 = random.nextInt(11) + 1;
   final int num4 = random.nextInt(11) + 1;
   switch (temp) {
            case 2://2组数字
                switch (signal) {
                    case 0:// +
                        tv_math.setText(num1 + "+" + num2 + "=?");
                        btn_answers.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                tv_math.setText(num1 + "+" + num2 + "=" + (num1 + num2));
                            }
                        });
                        break;
                    case 1:// -
                        tv_math.setText(num1 + "-" + num2 + "=?");
                        btn_answers.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                tv_math.setText(num1 + "-" + num2 + "=" + (num1 - num2));
                            }
                        });
                        break;
                    case 2:// ×
                        tv_math.setText(num1 + "*" + num2 + "=?");
                        btn_answers.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                tv_math.setText(num1 + "*" + num2 + "=" + (num1 * num2));
                            }
                        });
                        break;
 
                    case 3:// ÷
                        tv_math.setText(num1 + "/" + num2 + "=?");
                        btn_answers.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                tv_math.setText(num1 + "/" + num2 + "=" + (num1 / num2));
                            }
                        });
                        break;
                }
                break;
 
            case 3://3组数字
                switch (signal) {//第一个运算符
                    case 0:// +
                        switch (signal1) {//第二个运算符
                            case 0:// +
                                tv_math.setText(num1 + "+" + num2 + "+" + num3 + "=?");
                                btn_answers.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        tv_math.setText(num1 + "+" + num2 + "+" + num3 + "=" + (num1 + num2 + num3));
                                    }
                                });
                                break;
 
                            case 2:// *
                                switch (signal1) {//第二个运算符
                                    case 1:// -
                                        tv_math.setText(num1 + "*" + num2 + "-" + num3 + "=?");
                                        btn_answers.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                tv_math.setText(num1 + "*" + num2 + "-" + num3 + "=" + (num1 * num2 - num3));
                                            }
                                        });
                                        break;
 
 
                                }
 
                         break;
 
                  case 4://4组数字
                     switch (signal) {//第一个运算符
                        case 0:// +
                          switch (signal1) {//第二个运算符
                             case 0:// +
                                   switch (signal2) {//第三个运算符
                                      case 0:// +
                                        tv_math.setText(num1 + "+" + num2 + "+" + num3 + "+" + num4 + "=?");
                                        btn_answers.setOnClickListener(new View.OnClickListener() {
                                              @Override
                                         public void onClick(View v) {
                                               tv_math.setText(num1 + "+" + num2 + "+" + num3 + "+" + num4 + "=" + (num1 + num2 + num3 + num4));
 
                                                            }
 
                                          });
                                           break;
                                           case 1:// -
                                           switch (signal1) {//第二个运算符
                                           case 3:// /
                                           switch (signal2) {//第三个运算符
                                           case 2:// *
                                            tv_math.setText(num1 + "-" + num2 + "/" + num3 + "*" + num4 + "=?");
                                               btn_answers.setOnClickListener(new View.OnClickListener() {
                                                  @Override
                                                   public void onClick(View v) {
                                           tv_math.setText(num1 + "-" + num2 + "/" + num3 + "*" + num4 + "=" + (num1 - num2 / num3 * num4));
                                         }
                                    });
                              }
 
                          }
               break;
              }
           break;
         }
 
       }
      }
     }
    }
  }
}

Android随机给出加减乘除的四则运算方法

Android随机给出加减乘除的四则运算方法

到此,关于“Android随机给出加减乘除的四则运算方法”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI