Andorid Studio 实现两个随机数的加减乘除

首先将布局改为线性布局
我这里实现的是两个随机数的加法运算,其他运算只需将加法改为对应方法即可。

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="两个随机数的加法运算"
        android:textSize="35sp"
        android:textColor="#E91E63"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="50dp"
        >
        <EditText
            android:id="@+id/et_1"
            android:layout_width="80dp"
            android:layout_height="60dp"
            android:textSize="30sp"
            android:layout_marginTop="10dp"
            android:enabled="false"
            android:textColor="#03A9F4"
            />

        <TextView
            android:layout_width="40dp"
            android:layout_height="60dp"
            android:textStyle="bold"
            android:text="+"
            android:textSize="40sp"
            android:layout_gravity="center"
            />

        <EditText
        android:id="@+id/et_2"
        android:layout_width="80dp"
        android:layout_height="60dp"
        android:textSize="30sp"
        android:layout_marginTop="10dp"
        android:enabled="false"
        android:textColor="#03A9F4"
        />
        <TextView
            android:layout_width="40dp"
            android:layout_height="60dp"
            android:textStyle="bold"
            android:text="="
            android:textSize="40sp"
            android:layout_gravity="center"
            />
        <EditText
            android:id="@+id/et_3"
            android:layout_width="80dp"
            android:layout_height="60dp"
            android:textSize="30sp"
            android:layout_marginTop="10dp"
            android:text=""
            android:textColor="#03A9F4"
            />

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="60dp">

        <Button
            android:id="@+id/btn_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="开始出题"
            android:textSize="30sp"
            android:layout_marginLeft="10dp"
            />
        <Button
            android:id="@+id/btn_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="判断正误"
            android:textSize="30sp"
            android:layout_marginLeft="40dp"
            />

    </LinearLayout>


</LinearLayout>

 

将具体布局设置好后便可以开始进行后续操作

这些是在操作过程中需要导入的包

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Random;

下面进行按钮和配置

public class MainActivity extends AppCompatActivity implements OnClickListener{
    private Button mBut1;
    private Button mBut2;
    private Random mRandom;
    private EditText mEdit1;
    private EditText mEdit2;
    private EditText mEdit3;
    private int x,y;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.text1);
        mBut1=findViewById(R.id.btn_1);
        mBut2=findViewById(R.id.btn_2);
        mEdit1=findViewById(R.id.et_1);
        mEdit2=findViewById(R.id.et_2);
        mEdit3=findViewById(R.id.et_3);
        mBut1.setOnClickListener(this);
        mBut2.setOnClickListener(this);
        mRandom=new Random();  //创建对象
        myRandom(); //声明获取随机数的方法
        mEdit3.requestFocus(); //让et_3处于焦点状态
    }
    private void myRandom() {
        x=mRandom.nextInt(100)+1;
        y=mRandom.nextInt(100)+1;
    }

    @Override
    public void onClick(View v) {
        String dite3=mEdit3.getText().toString();
        switch(v.getId()){
            case R.id.btn_1:{//开始出题
                myRandom();
                mEdit1.setText(String.valueOf(x));
                mEdit2.setText(String.valueOf(y));
            }
            break;
            case R.id.btn_2://判断正误
                int result = Integer.parseInt(dite3);//Integer.parseInt(String s)返回整数值
                if (result == x + y) {
                    Toast.makeText(MainActivity.this, "回答正确", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "回答错误", Toast.LENGTH_SHORT).show();
                    mEdit3.setText("");
                }
                break;
        }
    }
}

下面是运行结果:

点击开始出题会出现两个随机数 在et_3中输入答案

4b624c63ef6f405395d5ad74bd7dd860.png

 如果输入正确会出现“回答正确”,如果错误会出现“回答错误”并会将自己输入的结果清空;

 

到此,一个简单的小玩意儿就完成了,欢迎评论区留言。

 

 

package ymq.demo03; import android.app.Activity; import android.os.Bundle; import android.view.*; import android.widget.*; public class demo03 extends Activity { /** Called when the activity is first created. */ String str=""; EditText et; int c=0,flag=0; double b=0.0,g=0.0,f=0.0; View vi; public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub menu.add(0, 1, 1, "退出"); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub if(item.getItemId()==1){finish();} return super.onOptionsItemSelected(item); } //计算方法 public double calculater(){ switch(c){ case 0:f=g;break; case 1:f=b+g;break; case 2:f=b-g;break; case 3:f=b*g;break; case 4:f=b/g;break; } b=f; c=0; return f; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获得按键 final Button number[]=new Button[10]; final Button fuhao[]=new Button[11]; fuhao[0]=(Button)findViewById(R.id.button01); fuhao[1]=(Button)findViewById(R.id.button02); fuhao[2]=(Button)findViewById(R.id.button03); fuhao[3]=(Button)findViewById(R.id.button04); fuhao[4]=(Button)findViewById(R.id.button05); fuhao[5]=(Button)findViewById(R.id.button06); fuhao[6]=(Button)findViewById(R.id.buttonce); fuhao[7]=(Button)findViewById(R.id.buttonc); fuhao[8]=(Button)findViewById(R.id.zheng); fuhao[9]=(Button)findViewById(R.id.kaifang); fuhao[10]=(Button)findViewById(R.id.pingfang); number[0]=(Button)findViewById(R.id.button0); number[1]=(Button)findViewById(R.id.button1); number[2]=(Button)findViewById(R.id.button2); number[3]=(Button)findViewById(R.id.button3); number[4]=(Button)findViewById(R.id.button4); number[5]=(Button)findViewById(R.id.button5); number[6]=(Button)findViewById(R.id.button6); number[7]=(Butto
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值