zoukankan      html  css  js  c++  java
  • Android猜数字大小游戏

    功能介绍:该程序能够提示猜大了猜小了,并且对空白输入处理,还对猜测次数限制,提供重置功能

    1、先看界面,一个输入框EditText,两个Button

    2、界面设计  activity_main2.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".Main2Activity">
    
        <EditText
            android:id="@+id/editText2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number" />
    
        <Button
            android:id="@+id/button16"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="提交" />
    
        <Button
            android:id="@+id/button17"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="重新开始" />
    </LinearLayout>

    3、逻辑事件控制 Main2Activity.java

    package com.example.myapplication;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    import java.util.Random;
    
    
    
    public class Main2Activity extends AppCompatActivity {
        private Button sure=null;
        private Button reset=null;
        int num = 5; //机会次数
        private EditText input_object=null;
        Random random = new Random(10);
        int rand = random.nextInt(10);
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
    
            sure = (Button) findViewById(R.id.button16);
            reset = (Button) findViewById(R.id.button17);
            input_object = (EditText) findViewById(R.id.editText2);
    
            sure.setOnClickListener(new View.OnClickListener() {//监听确认按钮
                @Override
                public void onClick(View v) {
                    String empt=input_object.getText().toString().trim();//提前检测输入是否为空
    
                    if(num>0) {//机会用完就不能猜了
                        if (empt.isEmpty()) {//判断输入是否为空
                            Toast.makeText(Main2Activity.this, "你大爷的!别乱点。。。", Toast.LENGTH_LONG).show();
                        } else {
                            num--;//机会减少
                            int inputnum = (int) Integer.parseInt(input_object.getText().toString());//如果输入为空会产生致命影响
                            if (inputnum == rand) {//猜对
                                Toast.makeText(Main2Activity.this, "牛逼啊大爷!猜对了。。。", Toast.LENGTH_LONG).show();
                            } else if (inputnum < rand) {//猜小
                                Toast.makeText(Main2Activity.this, "猜小了。。。还有" + num+"次机会", Toast.LENGTH_LONG).show();
                            } else {//猜大
                                Toast.makeText(Main2Activity.this, "猜大了。。。还有" + num+"次机会", Toast.LENGTH_LONG).show();
                            }
                        }
                    }else{//机会用完提示
                        Toast.makeText(Main2Activity.this, "大爷你玩完了", Toast.LENGTH_LONG).show();
                    }
                }
            });
    
            reset.setOnClickListener(new View.OnClickListener() {//监听重置按钮
                @Override
                public void onClick(View v) {
                    rand = random.nextInt(10);//重新产生随机数
                    num=5;//机会重置
                    Toast.makeText(Main2Activity.this, "随机数重置", Toast.LENGTH_LONG).show();
                }
            });
        }
    }
  • 相关阅读:
    Tarjan算法求双连通分量
    Tarjan
    前端技术实现js图片水印
    记录一下ionic canvas图片,还有canvas里面的图片跨域的问题
    ionic cordova screenshot 使用和操作
    关于ionic2 更新到ionic3 后组件不能用的解决方案
    背景图处理,这是个好东西记录一下
    radio样式的写法,单选和多选如何快速的改变默认样式,纯CSS,
    ionic使用cordova插件中的Screenshot截图分享功能
    ionic中执行pop返回上一个页面,还需要执行操作
  • 原文地址:https://www.cnblogs.com/easyidea/p/10498688.html
Copyright © 2011-2022 走看看