猜數(shù)游戲java實(shí)現(xiàn)代碼
本文實(shí)例為大家分享了java實(shí)現(xiàn)猜數(shù)游戲的具體代碼,供大家參考,具體內(nèi)容如下
有開(kāi)始界面,可以設(shè)置范圍,設(shè)置猜的次數(shù)
代碼如下:
public static void main(String[] args) { Random rand = new Random(); Scanner sc = new Scanner(System.in); System.out.println('----------猜數(shù)游戲----------'); System.out.println(' 1.開(kāi)始游戲n 2.游戲設(shè)置n 3.退出游戲'); System.out.println('請(qǐng)輸入:'); int[] arr = new int[3]; for (int i = 1; i < arr.length; i++) { if (i < 4) { arr[i] = sc.nextInt(); } break; } int a = 0; //開(kāi)始界面 for (int i = 1; i < arr.length; i++) { if (arr[i] == 1) { System.out.println('開(kāi)始游戲'); a = 1; break; } else if (arr[i] == 2) { System.out.println('游戲設(shè)置'); a = 2; break; } else if (arr[i] == 3) { System.out.println('退出游戲'); a = 3; break; } else System.out.println('錯(cuò)誤輸入'); } //設(shè)置界面 int max = 100; int min = 0; int cishu = 5; if (a == 2) { System.out.println('設(shè)置范圍'); System.out.println('最小值'); min = sc.nextInt(); System.out.println('最大值'); max = sc.nextInt(); System.out.println('設(shè)置猜的次數(shù)'); cishu = sc.nextInt(); //int randNum = rand.nextInt(max - min + 1) + min; System.out.println('設(shè)置范圍n最小值為:' + min + 'n最大值為:' + max + 'n猜的次數(shù)為:' + cishu); a = 6; } //設(shè)置后回到開(kāi)始界面 if (a==6){ System.out.println('----------猜數(shù)游戲----------'); System.out.println(' 1.開(kāi)始游戲n 2.游戲設(shè)置n 3.退出游戲'); System.out.println('請(qǐng)輸入:'); for (int i = 1; i < arr.length; i++) { if (i < 4) { arr[i] = sc.nextInt(); } break; } //開(kāi)始界面 for (int i = 1; i < arr.length; i++) { if (arr[i] == 1) { System.out.println('開(kāi)始游戲'); a = 1; break; } else if (arr[i] == 2) { System.out.println('游戲設(shè)置'); a = 2; break; } else if (arr[i] == 3) { System.out.println('退出游戲'); a = 3; break; } else System.out.println('錯(cuò)誤輸入'); } } //游戲界面 if (a == 1) { int randNum = rand.nextInt(max - min + 1) + min; System.out.println('我想了一個(gè)' + min + '到' + max + '的數(shù),請(qǐng)你來(lái)猜:n你有' + cishu + '次機(jī)會(huì)'); int i = 1; for (; i <= cishu; ++i) { System.out.println('第' + i + '次:'); int guess = sc.nextInt(); if (i < cishu) { if (randNum >= guess) { if (randNum <= guess) { System.out.println('恭喜你猜對(duì)了!n' + '該值是:' + randNum); break; } else System.out.println('你猜小了!'); } else System.out.println('你猜大了!'); } else System.out.println('你猜錯(cuò)了!n該值是:' + randNum); } }}
運(yùn)行結(jié)果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. php使用正則驗(yàn)證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)2. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)車(chē)輛管理系統(tǒng)3. HTML5實(shí)戰(zhàn)與剖析之觸摸事件(touchstart、touchmove和touchend)4. ASP將數(shù)字轉(zhuǎn)中文數(shù)字(大寫(xiě)金額)的函數(shù)5. Jsp servlet驗(yàn)證碼工具類(lèi)分享6. jscript與vbscript 操作XML元素屬性的代碼7. 基于PHP做個(gè)圖片防盜鏈8. Jsp+Servlet實(shí)現(xiàn)文件上傳下載 文件列表展示(二)9. asp.net core 認(rèn)證和授權(quán)實(shí)例詳解10. XML在語(yǔ)音合成中的應(yīng)用
