zoukankan      html  css  js  c++  java
  • 题目--猜数字

    import java.util.Random;
    import java.util.Scanner;
    
    public class Test21{
    	//猜数字
    	public static void main(String[] args){
    		Random r=new Random();
    		int number=r.nextInt(100)+1;//随机一个1-100的数
    		for(;;){
    			Scanner sc=new Scanner(System.in);
    			int i=sc.nextInt();
    			if(i==number){
    				System.out.println("你猜中了");
    				break;
    			}else if(i<number){
    				System.out.println("你猜的数据小了");
    			}else{
    				System.out.println("你猜的数据大了");
    			}
    		}
    		System.out.println("number="+number);
    	}
    }
    

    用while实现更合适

    import java.util.Random;
    import java.util.Scanner;
    
    public class Test22{
    	//猜数字
    	public static void main(String[] args){
    		Random r=new Random();
    		int number=r.nextInt(100)+1;//随机一个1-100的数
    		while(true){
    			Scanner sc=new Scanner(System.in);
    			int i=sc.nextInt();
    			if(i==number){
    				System.out.println("你猜中了");
    				break;
    			}else if(i<number){
    				System.out.println("你猜的数据小了");
    			}else{
    				System.out.println("你猜的数据大了");
    			}
    		}
    		System.out.println("number="+number);
    	}
    }
    
  • 相关阅读:
    网络知识 ACL NAT IPv6
    const用法
    单向链表排序
    文件系统
    protel DXP的类矢量图功能
    proteus画元件
    SD卡FAT32文件系统格式
    如何实现一个malloc函数
    sbrk and coreleft
    windows下常用快捷键
  • 原文地址:https://www.cnblogs.com/minconding/p/13430998.html
Copyright © 2011-2022 走看看