就是韩国教授让做一个小程序,初学者老师也没有教,不太会
加减,(1至99)随机生成
就好像是那种过关小游戏
答对下面就显示答对了
打错就显示错了
首先,你要了解android的随机数是怎么生成,其次,你要了解android中字符比较是用什么,至于加减都是数学问题,不难
xml:
<?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:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/ll_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_before"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_weight="1"
android:gravity="center"
android:textSize="24sp"
tools:text="10" />
<TextView
android:id="@+id/tv_center"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textSize="24sp"
tools:text="+" />
<TextView
android:id="@+id/tv_after"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textSize="24sp"
tools:text="20" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="="
android:textSize="24sp" />
<EditText
android:id="@+id/et_result"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="30dp"
android:layout_weight="1"
android:digits="0123456789-"
android:gravity="center"
android:textSize="24sp"
tools:text="30" />
</LinearLayout>
<TextView
android:id="@+id/tv_opinion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ll_one"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:gravity="center"
android:hint="正确或者错误" />
<Button
android:id="@+id/btn_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_opinion"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="下一题"
android:textSize="24sp" />
<Button
android:id="@+id/btn_sure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_next"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="确定"
android:textSize="24sp" />
</RelativeLayout>
类:
package com.example.administrator.writeapp.shuzi_youxi.activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.example.administrator.writeapp.R;
/**
* Created by Administrator on 2019-9-27.
*/
public class ShuZiYouXiActivity extends AppCompatActivity {
TextView tv_before;
TextView tv_center;
TextView tv_after;
TextView tv_opinion;
EditText et_result;
Button btn_sure;
Button btn_next;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_youxi);
initViews();
}
private void initViews() {
tv_before = (TextView) findViewById(R.id.tv_before);
tv_center = (TextView) findViewById(R.id.tv_center);
tv_after = (TextView) findViewById(R.id.tv_after);
tv_opinion = (TextView) findViewById(R.id.tv_opinion);
et_result = (EditText) findViewById(R.id.et_result);
btn_sure = (Button) findViewById(R.id.btn_sure);
btn_next = (Button) findViewById(R.id.btn_next);
setTextView();
btn_sure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int youResult = Integer.valueOf(et_result.getText().toString());
SharedPreferences spf = getSharedPreferences("result",MODE_PRIVATE);
int ccResult = spf.getInt("jg",0);
if (youResult == ccResult){
tv_opinion.setText("正确");
}else{
tv_opinion.setText("错误");
}
}
});
btn_next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
et_result.setText("");
tv_opinion.setText("正确或错误");
setTextView();
}
});
}
public void setTextView(){
int before = (int) (Math.random()*99+1);
int after = (int) (Math.random()*99+1);
tv_before.setText(String.valueOf(before));
tv_after.setText(String.valueOf(after));
int center = (int) (Math.random()*4+1);
int result = 0;
switch (center){
case 1:
result = before + after;
tv_center.setText("+");
break;
case 2:
result = before - after;
tv_center.setText("-");
break;
case 3:
result = before * after;
tv_center.setText("*");
break;
case 4:
result = before / after;
tv_center.setText("/");
break;
default:
}
SharedPreferences spf = getSharedPreferences("result",MODE_PRIVATE);
SharedPreferences.Editor e = spf.edit();
e.putInt("jg",result);
e.apply();
}
}