91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android隨機給出加減乘除的四則運算方法

發布時間:2022-04-24 10:40:26 來源:億速云 閱讀:405 作者: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

泰宁县| 渭源县| 济南市| 广平县| 赤峰市| 朝阳区| 凤庆县| 宁都县| 鹤岗市| 康定县| 拉萨市| 巨野县| 鸡西市| 南川市| 乌拉特前旗| 石阡县| 无锡市| 衡阳县| 喀喇| 庐江县| 佛冈县| 靖州| 松阳县| 从江县| 贞丰县| 丹寨县| 渝中区| 沅陵县| 新邵县| 清镇市| 平乐县| 建阳市| 富蕴县| 辰溪县| 和林格尔县| 榆树市| 弥勒县| 临猗县| 富宁县| 阳高县| 扶绥县|