今天休息,学习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;
}
}
}
}
}