| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- package homework.p08_guessgame;
- import java.util.Random;
- /**
- * @author WanJl
- * @version 1.0
- * @title GuessGame
- * @description
- *
- *
- * @create 2026/7/22
- */
- public class GuessGame {
- /**
- * 目标数字(系统随机生成)
- */
- private int target;
- /**
- * 最大范围(如 100 表示 1~100)
- */
- private int maxRange;
- /**
- * 已猜次数
- */
- private int attempts;
- public GuessGame(int maxRange) {
- Random random=new Random();
- this.maxRange = maxRange;
- //并将 attempts 初始化为 0
- this.attempts=0;
- target=random.nextInt(this.maxRange)+1;
- }
- /**
- * 猜数字的方法
- * @param num
- * @return
- */
- public String guess(int num){
- //已猜次数加1
- this.attempts++;
- if (num>target){
- return "猜大了";
- }else if(num<this.target){
- return "猜小了";
- }else{
- return "猜对了";
- }
- }
- /**
- * 返回已猜次数
- * @return
- */
- public int getAttempts(){
- return this.attempts;
- }
- public int getTarget() {
- return target;
- }
- public void setTarget(int target) {
- this.target = target;
- }
- public int getMaxRange() {
- return maxRange;
- }
- public void setMaxRange(int maxRange) {
- this.maxRange = maxRange;
- }
- public void setAttempts(int attempts) {
- this.attempts = attempts;
- }
- }
|