猜数字大小游戏
程序解讀:系統生成一個隨機數,用戶輸入一個數字,與系統生成的隨機數進行比較,并輸出用戶是否猜對,最終猜對時輸出用戶猜的總次數
代碼:
import java.util.Random; import java.util.Scanner;public class 猜數字大小 {public static void main(String[] args) {int i=(int)(Math.random()*100);boolean Y=true;int tonji=1;while (Y){Scanner scanner = new Scanner(System.in);System.out.println("請輸入數字:");int J=scanner.nextInt();if (i<J){System.out.println("猜的數據大了");++tonji;Y=true;}else if (i>J){System.out.println("猜的數據小了");++tonji;Y=true;}else {System.out.println("恭喜你猜中了,數字為:"+i);System.out.println("你一共猜了"+tonji+"次");System.out.println("游戲結束!");Y=false;}}} }?
運行效果:
萌新上路
希望可以對大家起到幫助!
也希望大佬可以給小萌新提出代碼優化建議!
總結
- 上一篇: 在查找预编译头时遇到意外的文件结尾。是否
- 下一篇: 激光SLAM建图过程中的问题Messag