Files
javaSE-0113/day07/src/com/inmind/random02/Demo06.java

43 lines
1.9 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.inmind.random02;
/*
常用类_Random_猜数字游戏(1~100)
分析:
1.使用random类获取一个随机值
2.使用scanner输入你所猜的值
a.将输入的猜的值跟随机数做比较如果猜的值大了需要提示猜的值33大了
b.将输入的猜的值跟随机数做比较如果猜的值小了需要提示猜的值33小了
c.将输入的猜的值跟随机数做比较,如果猜的值一致,需要提示正确,并结束游戏
3.注意:如果用户猜不对,程序永远不结束(死循环)
*/
import java.util.Random;
import java.util.Scanner;
public class Demo06 {
public static void main(String[] args) {
//1.使用random类获取一个随机值
int randomNum = new Random().nextInt(1, 101);
System.out.println("猜数字游戏开始您可以输入1~100的数字");
//2.使用scanner输入你所猜的值
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入您所猜的值:");
int guessNum = sc.nextInt();
//使用if格式三
if (guessNum > randomNum) {
//a.将输入的猜的值跟随机数做比较如果猜的值大了需要提示猜的值33大了
System.out.println("您猜的值:"+guessNum+"大了");
} else if (guessNum < randomNum) {
//b.将输入的猜的值跟随机数做比较如果猜的值小了需要提示猜的值33小了
System.out.println("您猜的值:" + guessNum + "小了");
} else {
//c.将输入的猜的值跟随机数做比较,如果猜的值一致,需要提示正确,并结束游戏
//提示猜对了
System.out.println("恭喜您,答对了!!");
//结束游戏
break;
}
}
}
}