java語言實(shí)現(xiàn)猜數(shù)字游戲
本文實(shí)例為大家分享了java實(shí)現(xiàn)猜數(shù)字游戲的具體代碼,供大家參考,具體內(nèi)容如下
隨機(jī)生成0~100的數(shù)字,通過控制臺(tái)輸入猜測(cè)數(shù)字,輸出進(jìn)行提示,知道猜測(cè)正確后,結(jié)束本次猜數(shù)字游戲,從而決定是否需要重新玩
代碼如下:
import java.util.Random;import java.util.Scanner;public class GuessNumber1{ public static Scanner scanner = new Scanner(System.in);//輸入 public static Random rnd = new Random();//產(chǎn)生隨機(jī)數(shù) public static void meau(){ System.out.println(' *****歡迎進(jìn)入猜數(shù)字游戲***** '); System.out.println(' 1:play----------0:exit '); System.out.println('********************************'); } public static void userplay(){ while(true){ meau(); System.out.print('請(qǐng)輸入你的選擇:'); int sc = scanner.nextInt(); switch(sc){ case 1:{ play(); break; } case 0:{ return; } default:{ System.out.println('輸入錯(cuò)誤'); break; } } }} public static void play(){ int random = rnd.nextInt(100); while(true){ System.out.print('請(qǐng)輸入你猜想的數(shù)字:'); int sc = scanner.nextInt(); if (sc<random){ System.out.println('猜小了'); }else if (sc>random){ System.out.println('猜大了'); }else{ System.out.println('猜對(duì)了'); break; } }} public static void main(String[] args){ userplay(); } }
對(duì)應(yīng)結(jié)果如下:

更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,也分享給大家:
C++經(jīng)典小游戲匯總
python經(jīng)典小游戲匯總
python俄羅斯方塊游戲集合
JavaScript經(jīng)典游戲 玩不停
java經(jīng)典小游戲匯總
javascript經(jīng)典小游戲匯總
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Springboot Druid 自定義加密數(shù)據(jù)庫密碼的幾種方案2. 詳解Android studio 動(dòng)態(tài)fragment的用法3. 解決Android studio xml界面無法預(yù)覽問題4. Spring Boot和Thymeleaf整合結(jié)合JPA實(shí)現(xiàn)分頁效果(實(shí)例代碼)5. 什么是python的自省6. Vuex localStorage的具體使用7. php模擬實(shí)現(xiàn)斗地主發(fā)牌8. Spring MVC+ajax進(jìn)行信息驗(yàn)證的方法9. Vue封裝一個(gè)TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn)10. 使用Android studio查看Kotlin的字節(jié)碼教程

網(wǎng)公網(wǎng)安備