今天学习了Random 和数组
public static void main(String[] args) {
Random r = new Random();
for (int x = 0; x < 10; x++) {
int number = r.nextInt(10);
System.out.println("number:" + number);
}
// 如何获取到一个1-100之间的随机数呢?
int i = r.nextInt(100) + 1;
System.out.println("i:" + i);
}
下面是一个猜数字的小游戏
public static void main(String[] args) {
Random r = new Random();
int number = r.nextInt(100) + 1;
while(true){
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要猜的数字(1-100):");
int guessNumber = sc.nextInt();
if (guessNumber > number) {
System.out.println("你猜的数据" + guessNumber + "大了");
} else if (guessNumber < number) {
System.out.println("你猜的数据" + guessNumber
+ "小了");
} else {
System.out.println("恭喜你,猜中了");
break;
}
}
}
{:8_472:}{:8_472:}{:8_472:}
|
|