欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java写猜数字小游戏

程序员文章站 2022-10-05 16:14:14
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 }