今日练习如下:
1、猜数字游戏
- 需要记录猜数的整个过程
- 根据猜测的次数进行评价
package com.han4;
import java.util.Scanner;
public class GuessGame {
private int target;
private int[] history = new int[15];
private int count;
private boolean bb = false;
public void setTarget(int target) {
this.target = target;
}
public void begin() {
Scanner sc = new Scanner(System.in);
while (count < 15) {
System.out.println("第" + (count + 1) + "次猜测,请输入数字:输入-1退出");
int tmp = sc.nextInt();
if (tmp == -1) {
System.exit(0);
}
String res = guess(tmp);
if ("成功".equals(res)) {
break;
} else {
System.out.println(res);
}
}
if (count < 5) {
System.out.println("牛");
} else if (count < 10) {
System.out.println("厉害");
} else if (count < 15) {
System.out.println("一般");
} else {
System.out.println("菜");
}
showHistory();
sc.close();
}
private String guess(int num) {
String res = "";
if (num == target) {
bb = true;
res = "成功";
} else if (num > target) {
res = "猜大了!";
} else {
res = "猜小了!";
}
history[count++] = num;
return res;
}
private void showHistory() {
if (bb)
Syst