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. Nginx+php配置文件及原理解析2. 解決啟動(dòng)django,瀏覽器顯示“服務(wù)器拒絕訪問”的問題3. JSP數(shù)據(jù)交互實(shí)現(xiàn)過程解析4. css3溢出隱藏的方法5. python virtualenv和flask安裝沒有名為flask的模塊6. java中throws實(shí)例用法詳解7. CSS3實(shí)現(xiàn)動(dòng)態(tài)翻牌效果 仿百度貼吧3D翻牌一次動(dòng)畫特效8. Opencv+Python識(shí)別PCB板圖片的步驟9. ASP.NET MVC獲取多級(jí)類別組合下的產(chǎn)品10. 關(guān)于HTML5的img標(biāo)簽
