使用Java實(shí)現(xiàn)一個(gè)不同難度(高、中、低)的猜數(shù)字游戲
前言
友友們是否玩過猜數(shù)字游戲,相比一定是玩過吧 ! ! !
而小編今天帶來是能夠選擇不同難度的貪吃蛇,友友們是否期待呢,下面就讓小編來開始吧 ?? ?? ??
一. 菜單打印
public static void Meau(){
System.out.println("****** 有以下三種難度 *********");
System.out.println("**** 1,高難度:猜五次 *********");
System.out.println("**** 2.中難度: 猜十次 *********");
System.out.println("**** 3.低難度:猜十五次 ********");
System.out.println("**** 0. 退出 游戲 ********");
}

上面的代碼表明
打印三種難度的提示信息 ,以便用戶選擇。
二. 游戲的維持
public static void game(int count){
Scanner in = new Scanner(System.in);
Random r=new Random();
int num= r.nextInt(100);
System.out.println("數(shù)字范圍為:1~100");
while (count != 0){
System.out.print("請輸入你要猜的數(shù)字:");
int guess=in.nextInt();
if (guess>num){
System.out.println("猜大了!");
}else if (guess<num){
System.out.println("擦小了!");
}else {
System.out.println("恭喜你,猜對了!");
break;
}
count --;
System.out.printf("你還能猜 %d 次!\n",count);
}
if (count==0){
System.out.println("很遺憾!你未能猜對,游戲結(jié)束...");
}
}
我們通過循環(huán)來進(jìn)行猜的次數(shù)
并判斷和實(shí)際數(shù)字的大小關(guān)系
魚式瘋言
隨機(jī)數(shù)的生成細(xì)節(jié)
1. 先new隨機(jī)數(shù)對象
// 先 new 一個(gè)隨機(jī)數(shù)的對象
Random r=new Random();
2. 確定范圍并接收
// 利用引用變量 int num= r.nextInt(100);
() 內(nèi)放的是100 代表 數(shù)字范圍是1~100
三. 邏輯功能選擇
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int choose=0;
do {
Meau();
System.out.print("請選擇你需要難度合法的數(shù)字:");
choose=in.nextInt();
switch (choose){
case 0:
System.out.println("游戲正在退出中...");
break;
case 1:
System.out.println("你選擇的是高難度:五次");
game(5);
break;
case 2:
System.out.println("你選擇的是中難度:十次");
game(10);
break;
case 3:
System.out.println("你選擇的是低難度:十五次");
game(15);
break;
default:
System.out.println("輸入不合法,請重新輸入");
break;
}
}while (choose != 0);
}

小編通過 選擇的數(shù)字來進(jìn)行游戲難度的進(jìn)入
4. 源代碼展示
class J3_20 {
public static void Meau(){
System.out.println("****** 有以下三種難度 *********");
System.out.println("**** 1,高難度:猜五次 *********");
System.out.println("**** 2.中難度: 猜十次 *********");
System.out.println("**** 3.低難度:猜十五次 ********");
System.out.println("**** 0. 退出 游戲 ********");
}
public static void game(int count){
Scanner in = new Scanner(System.in);
Random r=new Random();
int num= r.nextInt(100);
System.out.println("數(shù)字范圍為:1~100");
while (count != 0){
System.out.print("請輸入你要猜的數(shù)字:");
int guess=in.nextInt();
if (guess>num){
System.out.println("猜大了!");
}else if (guess<num){
System.out.println("擦小了!");
}else {
System.out.println("恭喜你,猜對了!");
break;
}
count --;
System.out.printf("你還能猜 %d 次!\n",count);
}
if (count==0){
System.out.println("很遺憾!你未能猜對,游戲結(jié)束...");
}
}
// 猜數(shù)字游戲
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int choose=0;
do {
Meau();
System.out.print("請選擇你需要難度合法的數(shù)字:");
choose=in.nextInt();
switch (choose){
case 0:
System.out.println("游戲正在退出中...");
break;
case 1:
System.out.println("你選擇的是高難度:五次");
game(5);
break;
case 2:
System.out.println("你選擇的是中難度:十次");
game(10);
break;
case 3:
System.out.println("你選擇的是低難度:十五次");
game(15);
break;
default:
System.out.println("輸入不合法,請重新輸入");
break;
}
}while (choose != 0);
}
}

魚式瘋言
提示頁面,難度選擇頁面,和游戲進(jìn)行頁面相互嵌套從而實(shí)現(xiàn)我們的三種不同難度的猜數(shù)字游戲
總結(jié)
- 菜單打?。?放入方法中簡單的打印
- 游戲的維持: 隨機(jī)數(shù)的生成,以及循環(huán)和判斷來實(shí)現(xiàn)游戲的不斷進(jìn)行
- 邏輯功能選擇: 難度的選擇就在我們主邏輯中實(shí)現(xiàn)
- 源代碼展示: 三大板塊聯(lián)合在一起的全新效果
到此這篇關(guān)于使用Java實(shí)現(xiàn)一個(gè)不同難度(高、中、低)的猜數(shù)字游戲的文章就介紹到這了,更多相關(guān)Java猜數(shù)字游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決springboot運(yùn)行出現(xiàn)錯(cuò)誤:找不到或無法加載主類com.xxxx.xxxx.Application問題
文章介紹了在服務(wù)器上運(yùn)行一個(gè)未使用的Java項(xiàng)目時(shí)遇到的“找不到或無法加載主類”錯(cuò)誤,并提供了兩種解決方法:通過Maven install或build …、Goals輸入install并跳過測試來重新構(gòu)建項(xiàng)目2024-11-11
Java中Connection timed out和Connection refused的區(qū)別講解
今天小編就為大家分享一篇關(guān)于Java中Connection timed out和Connection refused的區(qū)別講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-04-04
深入dom4j使用selectSingleNode方法報(bào)錯(cuò)分析
本篇文章是對dom4j使用selectSingleNode方法報(bào)錯(cuò)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
關(guān)于@Autowierd && @Resource 你真的了解嗎
這篇文章主要介紹了關(guān)于@Autowierd && @Resource的具體使用,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08

