国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

猜數字游戲GuessNumber version1.0

2019-11-14 22:10:48
字體:
來源:轉載
供稿:網友
猜數字游戲GuessNumber version1.0

2014-12-09

17:42:20

  系統給定一個四位數。您有5次猜數機會,每次您可輸入一個四位數,系統判斷后會給出一個字符串,字符串由T、F、C表示。T表示該位上的數字是正確的;C表示該位上的數字存在這個四位數中,但位置錯誤;F表示該位上的數字是錯誤的,即不存在這個四位數中。

  程序代碼如下:

 1 /**  2  * @author nlee 3  * @version 2:58:11 PM Dec 9, 2014 4  */ 5 package guessNum; 6  7 import java.util.Scanner; 8  9 public class GuessNum10 {11     public static void main(String[] args)12     {13         // 1.生成正確答案14         String correctAnswer = "";15         // 生成四個一位隨機數,并將其轉換成String類型16         for (int i = 0; i < 4; i++)17         {18             int a = (int) (Math.random() * 10);19             correctAnswer += a;20         }21         // System.out.PRintln(correctAnswer);22 23         Scanner in = new Scanner(System.in);24         // 輸入上限為5次25         int times = 5;26         for (int j = 0; j < times; j++)27         {28             System.out.println("You have " + (times - j) + " times, "29                     + "please entry your number(four figures):");30             // 2.輸入猜測的數字31             String num = in.next();32             // num與correctAnswer的比較結果33             String implynum = "";34             // 3.比較num與correctAnswer35             for (int i = 0; i < 4; i++)36             {37                 if (correctAnswer.charAt(i) == num.charAt(i))38                 {39                     implynum += "T";40                 }41                 else42                     // 判斷correctAnswer是否包含num.charAt(i)這個數字43                     if (correctAnswer.contains(((CharSequence) ("" + num44                             .charAt(i)))))45                     {46                         implynum += "C";47                     }48                     else49                     {50                         implynum += "F";51                     }52             }53             System.out.println(implynum);54             // 如果implynum等于“TTTT”,表明猜對了,結束循環55             if (implynum.equals("TTTT"))56             {57                 System.out58                         .println("/nCorrect Answer!/nCongratulations!/nYou have succeed!");59                 break;60             }61             else62                 // 如果implynum不等于“TTTT”, 并且猜測次數也達到上限,表明游戲失敗63                 if (j == (times - 1))64                 {65                     System.out.println("/nYou have failed!/nGame over!");66                     System.out.println("/nCorrect Answer is: " + correctAnswer);67                 }68                 else69                 {70                     System.out.println("Wrong Answer!/nPlease try again!/n");71                 }72         }73     }74 }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大洼县| 商水县| 屏南县| 西平县| 淳安县| 奉节县| 巴南区| 宁远县| 南安市| 南雄市| 周至县| 呼玛县| 崇义县| 邵阳市| 洞口县| 铁岭县| 宜兰县| 昔阳县| 莒南县| 海盐县| 江油市| 方正县| 红桥区| 芦溪县| 桐梓县| 万山特区| 专栏| 镇安县| 白水县| 南川市| 古浪县| 县级市| 龙井市| 茌平县| 江门市| 曲沃县| 湖南省| 门源| 卓资县| 南通市| 漳浦县|