猜数字大小游戏
程序解讀:系統(tǒng)生成一個(gè)隨機(jī)數(shù),用戶輸入一個(gè)數(shù)字,與系統(tǒng)生成的隨機(jī)數(shù)進(jìn)行比較,并輸出用戶是否猜對(duì),最終猜對(duì)時(shí)輸出用戶猜的總次數(shù)
代碼:
import java.util.Random; import java.util.Scanner;public class 猜數(shù)字大小 {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("請(qǐng)輸入數(shù)字:");int J=scanner.nextInt();if (i<J){System.out.println("猜的數(shù)據(jù)大了");++tonji;Y=true;}else if (i>J){System.out.println("猜的數(shù)據(jù)小了");++tonji;Y=true;}else {System.out.println("恭喜你猜中了,數(shù)字為:"+i);System.out.println("你一共猜了"+tonji+"次");System.out.println("游戲結(jié)束!");Y=false;}}} }?
運(yùn)行效果:
萌新上路
希望可以對(duì)大家起到幫助!
也希望大佬可以給小萌新提出代碼優(yōu)化建議!
總結(jié)
- 上一篇: 在查找预编译头时遇到意外的文件结尾。是否
- 下一篇: 激光SLAM建图过程中的问题Messag