用Random类随机生成一个0-100之内的数字,让人去猜。
import java.util.Scanner; import java.util.Random; public class game { public
static void main(String[] args) { Random a = new Random(); int data =
a.nextInt(100)+1; Scanner input = new Scanner (System.in); while(true) {
System.out.println("你猜多大(0~100):"); int nc =input.nextInt(); if(nc<0 || nc>100)
{ System.out.println("输入的数字不在0-100之内"); }else if(nc<data){
System.out.println("猜小了"); }else if(nc>data) { System.out.println("猜大了"); }else
{ System.out.println("恭喜猜中!"); break; } } input.close();} }