zoukankan      html  css  js  c++  java
  • 有瑕疵的Android四则运算

    java文件:

    package com.example.dell_pc.homework;

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

    import java.util.Random;

    public class MainActivity extends AppCompatActivity {
        private int a;
        private int b;
        private int c;
        private int d;
        private int sy;
        private TextView tv;
        private Button btn_1;
        private Button btn_2;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //获取TextView,Button对象
            tv = (TextView) super.findViewById(R.id.textView5);
            btn_1 = (Button) super.findViewById(R.id.btn_1);
            btn_2 = (Button) super.findViewById(R.id.btn_2);
            random();
            view();

            btn_2.setOnClickListener(new Button.OnClickListener() {
                @Override

    //算法
                public void onClick(View v) {

                    switch (sy) {
                        case 1:
                            tv.setText(a + "+" + b + "-" + c + "*" +d+ "=?");
                            break;
                        case 2:
                            tv.setText(a + "-" + b + "*" + c + "/" + d+"=?");
                            break;
                        case 3:
                            tv.setText(a + "*" + b + "+" + c + "-" + d+"=?");
                            break;
                        case 4:
                            tv.setText(a + "/" + b + "-" + c + "+" + d+"=?");
                            break;
                        case 5:
                            tv.setText(a + "+" + b + "/" + c + "+" + d+"=?");
                            break;
                        case 6:
                            tv.setText(a + "+" + b + "/" + c + "+" + d+"=?");
                            break;
                        case 7:
                            tv.setText(a + "-" + b + "/" + c + "*" + d+"=?");
                            break;
                    }
                }

            });
    //事件监听
            btn_1.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    random();
                    view();
                }
            });
        }

    //生成100以内的随机数
        private void random() {
            a = new Random().nextInt(100);
            b = new Random().nextInt(100);
            c = new Random().nextInt(100);
            sy = new Random().nextInt(4);
        }

        private void view() {
            switch (sy) {
                case 1:
                    tv.setText(a + "+" + b + "-" + c + "*" +d+ "="+(a+b-c*d));
                    break;
                case 2:
                    tv.setText(a + "-" + b + "*" + c + "/" + d+"="+(a-b*c/d));
                    break;
                case 3:
                    tv.setText(a + "*" + b + "+" + c + "-" + d+"="+(a*b+c-d));
                    break;
                case 4:
                    tv.setText(a + "/" + b + "-" + c + "+" + d+"="+(a/b-c+d));
                    break;
                case 5:
                    tv.setText(a + "+" + b + "/" + c + "+" + d+"="+(a+b/c+d));
                    break;
                case 6:
                    tv.setText(a + "+" + b + "/" + c + "+" + d+"="+(a+b/c+d));
                    break;
                case 7:
                    tv.setText(a + "-" + b + "/" + c + "*" + d+"="+(a-b/c*d));
                    break;
            }
        }
    }

    布局文件:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.dell_pc.homework.MainActivity">


        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/textView5"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="108dp">

            <Button
                android:text="下一题"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btn_1"
                android:layout_weight="1" />

            <Button
                android:text="答案"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btn_2"
                android:layout_weight="1" />
        </LinearLayout>

        <TextView
            android:text="TextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView5"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="38dp" />
    </RelativeLayout>

     

  • 相关阅读:
    k8s--容器挂载 error: /proc must be mounted
    mysql--read only
    C#读取Excel文件(.xls .xlsx)
    如何使用BBCode
    markdown使用经验积累
    openlayers学习之-----入门篇
    echarts学习之----动态排序柱状图
    echarts学习之----多图例折线图
    Web3D学习之-----全景图预览插件photo-sphere-viewer
    vue报错解决----npm ERR!
  • 原文地址:https://www.cnblogs.com/qiushi123/p/6541033.html
Copyright © 2011-2022 走看看