欢迎光临散文网 会员登陆 & 注册

一个算数小游戏的代码

2020-08-24 18:41 作者:MogMouG  | 我要投稿

JAVA代码

package com.mycompany.myapp2;


import android.app.*;

import android.os.*;

import android.view.*;

import android.view.View.*;

import android.widget.*;

public class MainActivity extends Activity 

{

    

 int max=10,min=1;

 int jiashu1 = (int) (Math.random() * (max - min) + min); 

 int jiashu2 = (int) (Math.random() * (max - min) + min);

 private TextView text = null;

 private Button btn = null;

 private EditText edit = null;

 private TextView exp = null;

    @Override

    protected void onCreate(Bundle savedInstanceState)

    {

  super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

  this.exp = (TextView)super.findViewById(R.id.exp);

  this.text = (TextView)super.findViewById(R.id.text);

  this.btn = (Button)super.findViewById(R.id.btn);

  this.edit = (EditText)super.findViewById(R.id.edit);

  text.setText(jiashu1+"+"+jiashu2+"=?");

  btn.setOnClickListener(new btnClick());

    }

 private class btnClick implements OnClickListener

 {

  public void onClick(View v)

  {

   int sum2 = (jiashu1 + jiashu2);

   int sum1 = Integer.valueOf(edit.getText().toString());

   //Toast.makeText(MainActivity.this,,Toast.LENGTH_SHORT).show();

   if (sum2==sum1)

   {

    exp.setText("🙂");

   }

   else

   {

    exp.setText("🙁");

   }

   int max=10,min=1;

   jiashu1 = (int) (Math.random() * (max - min) + min); 

   jiashu2 = (int) (Math.random() * (max - min) + min);

   text.setText(jiashu1+"+"+jiashu2+"=?");

  }

 }

}

XML文件

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

 xmlns:android="http://schemas.android.com/apk/res/android"

 android:layout_width="match_parent"

 android:layout_height="match_parent"

 android:orientation="vertical"

 android:id="@+id/mainLinearLayout1"

 android:alpha="1">


 <LinearLayout

  android:layout_height="wrap_content"

  android:layout_width="wrap_content"

  android:orientation="horizontal">


  <TextView

   android:layout_height="wrap_content"

   android:layout_width="wrap_content"

   android:text="1+1=?"

   android:id="@+id/text"

   android:textSize="35sp"/>


  <TextView

   android:layout_height="wrap_content"

   android:layout_width="wrap_content"

   android:text=""

   android:textSize="35sp"

   android:id="@+id/exp"/>


 </LinearLayout>


 <EditText

  android:layout_height="wrap_content"

  android:layout_width="wrap_content"

  android:ems="9"

  android:id="@+id/edit"

  android:hint="输入数字"

  android:inputType="number"

  style="@style/AppTheme"/>


 <Button

  android:layout_height="wrap_content"

  android:layout_width="wrap_content"

  android:text="提交"

  android:id="@+id/btn"/>


</LinearLayout>


代码都是自己写的

大佬勿喷QAQ

一个算数小游戏的代码的评论 (共 条)

分享到微博请遵守国家法律