随机生成一个1到100的数字,然后让你猜想。
一共有十次机会,会提示你大了,还是小了。
玩玩吧。
View Code JAVA
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import java.util.*; public class HomeWork3_2 { public static void main(String[] args) { Random r = new Random(); int x = r.nextInt(100)+1; Scanner sc = new Scanner(System.in); System.out.print("随机数已经生成!\n请输入你猜想的数(1-100):"); int y,m=0; y = sc.nextInt(); while(true){ if (m==10) { System.out.println("你太笨了,下次再来吧!"); break; } if(y>x){ System.out.print("大了!再小一点!\n请重新输入:"); y = sc.nextInt(); m++; } else if (y |