男儿欲遂平生志,五经勤向窗前读。这篇文章主要讲述有瑕疵的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>
【有瑕疵的Android四则运算】
文章图片
文章图片
推荐阅读
- 简单的Android四则运算
- Frameworks detected: Android framework is detected in the project
- Android 6.0版本以后运行时权限提醒
- Android的学习总结
- android 编译打包烧录
- Android 软键盘控制方法以及开发中遇到的一些问题。转
- Android 系统中,那些能大幅提高工作效率的 API 汇总(持续更新中...)
- 浅谈Android保护技术__代码混淆
- ML(T分布随机邻居嵌入(t-SNE)算法)