1.9 Guess
简单猜数值游戏。
编写⼀个名为Guess的类。在其构造⽅法⽤系统时间对随机数⽣成器种⼦化 以防⽌每次⽣成的随机数序列相同(类java.util.Random);成员⽅法Result judge(int value)对⽤ 户从键盘上输⼊的整数值进⾏判断并返回结果。
编写Driver类,在⼊⼝点main()中实例化Guess的⼀ 个对象,提示⽤户输⼊⼀个整数。具体要求如下:
1. Guess的构造⽅法接收两个参数:bound,表示⽣成随机数的上界,默认值为10,即所⽣成 的整数在范围[0, 10)中(Random的int nextInt(int bound)⽅法);limit,表示允许⽤户猜测的 最⼤次数,默认值为10;在构造⽅法中⽤系统时间(System. currentTimeMillis()⽅法)作为随 机数⽣成器的种⼦;
2. 在成员⽅法void start()中,⽣成随机数并将其保存在成员变量value中,并将表示当前已猜 测次数的成员变量count复位;
3. 在成员⽅法Result judge(int value)中,递增⽤户已猜测的次数count,对⽤户所输⼊的整数 进⾏判断,并将判断结果以枚举Result(位于Guess类中)er 返回(EQUAL,value == this.value;LESS,this.value > value;GREAT,this.value < value;EXCEED,count >= limit)。
4. 在Driver类中,编写⼀个名为void guess(Guess guess)的⽅法,提示⽤户输⼊⼀个整数, 并调⽤Guess的judge()成员⽅法对⽤户所输⼊的整数进⾏判断,根据其返回值在终端(控制台) 上输出:
① 正确(EQUAL):“congratulation!”
② 太⼩(LESS):“Sorry,too small,remaining:XXX”
③ 太⼤(GREAT):“Sorry,too large,remaining:XXX”
④ 超限(EXCEED):“Sorry,exhausted,value:YYY” 其中XXX表示剩余的猜测次数(提示:在Guess中提供成员⽅法以访问其私有数据成员)⽽ YYY表示未被猜中的随机值。在太⼩或太⼤情况下,允许⽤户继续猜测。
5. 在main()⽅法中创建Guess的⼀个对象,在⼀个循环中,开始游戏,并在每⼀轮游戏之后询 问⽤户是否继续,如果⽤户回答否,游戏结束。
Java代码实现:import java.util.Random; import java.util.Scanner; public class Driver { public static void main(String[] args) { Guess g = new Guess(10,10); Scanner sc = new Scanner(System.in); boolean isFlag = true; while(isFlag){ g.start(); System.out.println(g.getValue()); guess(g); System.out.println("是否继续?Y/N"); String a = sc.nextLine(); if(!a.equals("Y")){ isFlag = false; } } System.out.println("游戏结束"); } public static void guess(Guess guess){ int value; String a = new String(); while(true){ System.out.println("请输入一个整数:"); Scanner sc = new Scanner(System.in); value = sc.nextInt(); a = guess.judge(value).getResultDesc(); if(a.equals("0")){ //字符串用equals进行判断 System.out.println("正确(EQUAL):“congratulation!" + "”"); break; } if(a.equals("1")){ System.out.println(guess.getLimit()); System.out.println(guess.getCount()); System.out.println("太⼩(LESS):“Sorry,too small,remaining:" + (guess.getLimit()-guess.getCount()) + "”"); } if(a.equals("2")){ System.out.println(" 太⼤(GREAT):“Sorry,too large,remaining:" + (guess.getLimit()-guess.getCount()) + "”"); } if(a.equals("3")){ System.out.println(" 超限(EXCEED):“Sorry,exhausted,value:" + guess.getValue() + "”"); break; } } } } class Guess{ private int bound; private int limit; private int value; private long t; private int count; public Guess(int bound, int limit) { this.bound = bound; this.limit = limit; t = System.currentTimeMillis(); } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } public int getLimit() { return limit; } public void setLimit(int limit) { this.limit = limit; } public void start(){ Random rd = new Random(t); this.value = rd.nextInt(bound); count = 0; } public Result judge(int value){ if(count >= limit){ count++; return Result.EXCEED; } else if(value == this.value){ count++; return Result.EQUAL; } else if(this.value > value){ count++; return Result.LESS; } else if(this.value < value){ count++; return Result.GREAT; } return Result.EQUAL; } enum Result{ EQUAL("0","congratulation!" ), LESS("1","Sorry,too small,remaining:" ), GREAT("2","Sorry,too large,remaining:" ), EXCEED("3","Sorry,exhausted,value:"); private final String ResultName; private final String ResultDesc; private Result(String resultDesc,String resultName) { ResultName = resultName; ResultDesc = resultDesc; } public String getResultName() { return ResultName; } public String getResultDesc() { return ResultDesc; } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)