循环-20. 猜数字游戏(15)
生活随笔
收集整理的這篇文章主要介紹了
循环-20. 猜数字游戏(15)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
猜數(shù)字游戲是令系統(tǒng)隨機產(chǎn)生一個100以內的正整數(shù)。用戶輸入一個數(shù)對其進行推測。須要你編敲代碼自己主動對其與隨機產(chǎn)生的被猜數(shù)進行比較。并提示大了(“Too big”),還是小了(“Too small”),相等表示猜到了。
假設猜到,則結束程序。
程序還要求統(tǒng)計猜的次數(shù)。假設1次猜出該數(shù),提示“Bingo!”;假設3次以內猜到該數(shù),則提示“Lucky You!”。假設超過3次可是在N(>3)次以內(包含第N次)猜到該數(shù),則提示“Good Guess!”;假設超過N次都沒有猜到,則提示“Game Over”。并結束程序。假設在到達N次之前。用戶輸入了一個負數(shù),也輸出“Game Over”。并結束程序。
輸入格式:
輸入第一行中給出2個不超過100的正整數(shù),各自是系統(tǒng)產(chǎn)生的隨機數(shù)、以及推測的最大次數(shù)N。最后每行給出一個用戶的輸入,直到出現(xiàn)負數(shù)為止。
輸出格式:
在一行中輸出每次推測對應的結果。直到輸出猜對的結果或“Game Over”則結束。
輸入例子: 58 4 70 50 56 58 60 -2 輸出例子: Too big Too small Too small Good Guess!import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner cin = new Scanner(System.in);int num = cin.nextInt();int n = cin.nextInt();int count = 0;while (cin.hasNext()) {count++;int nextNum = cin.nextInt();if (count > n || nextNum < 0) {System.out.println("Game Over");break;} else {if (nextNum > num) {System.out.println("Too big");} else if (nextNum < num) {System.out.println("Too small");} else if (nextNum == num && count == 1) {System.out.println("Bingo!");break;} else if (nextNum == num && count <= 3) {System.out.println("Lucky You!");break;} else {System.out.println("Good Guess!");break;}}}} }
轉載于:https://www.cnblogs.com/lytwajue/p/7229595.html
總結
以上是生活随笔為你收集整理的循环-20. 猜数字游戏(15)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 设备宽度
- 下一篇: JQuery UI之Autocomple