今天主要编写了一个猜数字小游戏,给大家分享下:
package com.itheima;
import java.util.Random;
import java.util.Scanner;
public class RandomDemo {
public static void main(String[] args) {
Random r=new Random();
int number=r.nextInt(100)+1;
int count=0;
while(true){
count++;
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个1-100之间的数字:");
int number1=sc.nextInt();
if(number1==number){
System.out.println("恭喜你答对了,你一共用了"+count+"次");
break;
}else if(number1>number){
System.out.println("你输入的数大了,请重新输入:");
}else{
System.out.println("你输入的数小了,请重新输入:");
}
}
}
}
|
|