随机数猜数字

今天休息,学习Lambda表达式的时候,突然想写一个猜数字的

小白一个,大佬勿喷,有错的地方可以更正

package com.itheima;

import java.util.Random;
import java.util.Scanner;

public class Random_Demo {
    public static void main(String[] args) {
        long start=System.currentTimeMillis();
        int count=0;
        while (true) {
            System.out.println("请输入你的数字0-100");
            Scanner scanner =new Scanner(System.in);
            int i0 = scanner.nextInt();
            int i1 = new Random().nextInt(100);
            System.out.println("随机数为"+i1+"  "+"你的猜测数字为"+i0);
            if (i0==i1){
                if(count<=10){
                    System.out.println("运气上佳,适合买彩票"+"一共猜测了"+(count+1)+"次");
                    long end=System.currentTimeMillis();
                    System.out.println("用时"+(end-start)/1000+"秒");
                }else{
                    System.out.println("虽然猜对了,但是你一共猜测了"+(count+1)+"次");
                    long end=System.currentTimeMillis();
                    System.out.println("用时"+(end-start)/1000+"秒");
                }
                break;
            }else{
                count++;
                System.out.println("第"+count+"次"+"猜错了,继续猜测");
                System.out.println();
                if(count==100){
                    System.out.println("你已经错误100次了,今天不适合买彩票");
                    System.out.println("下次见,拜拜,祝你好运");
                    break;
                }
            }
        }
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值