日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android——猜数字小游戏

發布時間:2023/12/15 Android 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android——猜数字小游戏 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
實現功能:總共分三個模式 普通模式:數字0-100 困難模式:數字0-1000 地獄模式:數字0-10000 輸入數字,會提示大還是小,猜對了,三種提示,還會有猜測數字顯示

MainActivity.java代碼如下:

public class MainActivity extends AppCompatActivity {private Button General,Diffculty,Hell;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);InitView();Listener();}private void InitView(){General = findViewById(R.id.general);Diffculty = findViewById(R.id.difficulty);Hell = findViewById(R.id.hell);}private void Listener(){OnClick onClick = new OnClick();General.setOnClickListener(onClick);Diffculty.setOnClickListener(onClick);Hell.setOnClickListener(onClick);}private class OnClick implements View.OnClickListener{@Overridepublic void onClick(View v) {Intent intent = null;switch (v.getId()){case R.id.general:intent = new Intent(MainActivity.this,General.class);break;case R.id.difficulty:intent = new Intent(MainActivity.this,Difficulty.class);break;case R.id.hell:intent = new Intent(MainActivity.this,Hell.class);break;}startActivity(intent);}} }

activity_main.xml:效果圖如下:

activity_main.xml:代碼如下:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="模式選擇"android:textSize="20sp"android:gravity="center"android:textColor="#000"/><Viewandroid:layout_width="match_parent"android:layout_height="1dp"android:background="#000"android:layout_marginTop="40dp"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_marginTop="70dp"><Buttonandroid:id="@+id/general"android:layout_width="100dp"android:layout_height="wrap_content"android:text="普通模式(1-100)" /><Buttonandroid:id="@+id/difficulty"android:layout_width="100dp"android:layout_height="wrap_content"android:text="困難模式(1-1000)"android:layout_gravity="center"android:layout_marginLeft="60dp"/><Buttonandroid:id="@+id/hell"android:layout_width="100dp"android:layout_height="wrap_content"android:text="地獄模式(1-10000)"android:layout_marginLeft="50dp"/></LinearLayout></RelativeLayout>

建立三個活動分別為General,Difficulty,Hell,由于代碼相似,只展示普通模式的代碼,其余倆只更改隨機函數中的數值范圍即可
General.java代碼如下:

public class General extends AppCompatActivity {private EditText Number;private Button Submit;private TextView GuessNumberResult, GuessNumberTimes;private int intGuessNumber = 0;private int NumberOfGuesses = 0;private int RandomNumber = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_general);Number = findViewById(R.id.Number);Submit = findViewById(R.id.submit);GuessNumberResult = findViewById(R.id.GuessNumberResult);GuessNumberTimes = findViewById(R.id.GuessNumberTimes);RandomNumber = (int) (Math.random()*(100-1+1));Submit.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(final View v) {String StrGuessNumber = Number.getText().toString();try{intGuessNumber = Integer.parseInt(StrGuessNumber);}catch (NumberFormatException e){e.printStackTrace();}if (intGuessNumber < RandomNumber ){NumberOfGuesses++;GuessNumberResult.setVisibility(View.VISIBLE);GuessNumberTimes.setVisibility(View.VISIBLE);GuessNumberResult.setText("猜的數字偏小");GuessNumberTimes.setText("猜測次數:"+NumberOfGuesses);}if (intGuessNumber > RandomNumber ){NumberOfGuesses++;GuessNumberResult.setVisibility(View.VISIBLE);GuessNumberTimes.setVisibility(View.VISIBLE);GuessNumberResult.setText("猜的數字偏大");GuessNumberTimes.setText("猜測次數:"+NumberOfGuesses);}if (intGuessNumber == RandomNumber ){NumberOfGuesses++;GuessNumberResult.setVisibility(View.VISIBLE);GuessNumberTimes.setVisibility(View.VISIBLE);GuessNumberResult.setText("恭喜你,猜對了");GuessNumberTimes.setText("猜測次數:"+NumberOfGuesses);}}});} }

activity_general.xml代碼如下:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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"tools:context=".General"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="普通模式"android:textSize="20sp"android:gravity="center"android:layout_marginTop="10dp"android:textColor="#000"/><Viewandroid:layout_width="match_parent"android:layout_height="1dp"android:background="#000"android:layout_marginTop="10dp"/><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid:id="@+id/Tips"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Please enter the number you selected:"android:layout_marginTop="100dp"/><EditTextandroid:id="@+id/Number"android:layout_width="100dp"android:layout_height="40dp"android:layout_marginTop="90dp"android:layout_alignRight="@+id/Tips"android:layout_marginRight="50dp"/></RelativeLayout><Buttonandroid:id="@+id/submit"android:layout_width="100dp"android:layout_height="50dp"android:text="Submit"android:textAllCaps="false"android:layout_marginTop="30dp"android:layout_gravity="center"/><TextViewandroid:id="@+id/GuessNumberResult"android:layout_height="wrap_content"android:layout_width="match_parent"android:visibility="invisible"android:layout_marginTop="10dp"android:layout_gravity="center"android:gravity="center"android:textColor="#ff0000"/><TextViewandroid:id="@+id/GuessNumberTimes"android:layout_width="match_parent"android:layout_height="wrap_content"android:visibility="invisible"android:layout_marginTop="20dp"android:layout_gravity="center"android:gravity="center"android:textColor="#ff0000"/></LinearLayout>

效果圖展示:

總結

以上是生活随笔為你收集整理的Android——猜数字小游戏的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。