Android加减乘除的四则运算算术题
DY.memory 人气:0点击显示题目即可在指定范围内随机出算数题目,适合初学者上手,易懂,另外,我没有把每一种运算的可能性都写出来,只写了部分的,不过两个数,三个数和四个数运算的可能性我都写了1-2中,你们可以比划模仿去写出其他的运算可能性,也很容易的,下面上代码!
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>
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; } } } } } } }
加载全部内容