猜拳小游戏
- 游戏规则:程序产生一个[1, 100]之间的随机整数,用户输入整数进行猜测,如果猜对了,就结束游戏;如果猜错了,程序会提示你是猜高了还是猜低了,然后让你继续输入整数进行猜测.
(一)编码实现
- 包里创建task06.
package t06;
import java.util.Random;
import java.util.Scanner;
/**
-
功能
-
lcc
-
日期:2022年04月14日
*/
public class Task06 {
public static void main(String[] args) {int target, x; Scanner sc = new Scanner(System.in); Random random = new Random(); target = random.nextInt(100); System.out.print("输入你猜测的整数:"); x = sc.nextInt(); while (x != target) { if (x > target) { System.out.println("朋友你猜高了,继续猜吧"); } else { System.out.println("朋友你猜低了,继续猜吧"); } System.out.print("输入一个[1,100]的整数:"); x = sc.nextInt(); } System.out.println("恭喜你猜对了,游戏结束!");
}
} -
运行程序,查看结果