Java写猜数字小游戏
程序员文章站
2022-04-28 09:52:17
1 import java.util.Random; //调用随机数包 2 import java.util.Scanner; 3 public class CSZ_demo{ 4 public static void main(String args[]){ 5 Random ran = new ... ......
1 import java.util.Random; //调用随机数包 2 import java.util.Scanner; 3 public class CSZ_demo{ 4 public static void main(String args[]){ 5 Random ran = new Random(); 6 int i = ran.nextInt(10); //随机数赋值给i 7 Scanner sc = new Scanner(); 8 while(ture){ 9 int j = sc.nextInt(System.in); //j接收来自键盘的输入 10 if(j<i){ 11 System.out.println("猜小了"); 12 } else if(j>i){ 13 System.out.println("猜大了"); 14 }else{ 15 System.out.println("猜对了"); 16 break; //猜对break跳出循环 17 } 18 19 } 20 21 } 22 }