public class GuessNumber {
static Scanner sc = new Scanner(System.in);
public static void guessNumber() {
ArrayList<Integer> arr = new ArrayList<>();
System.out.println("游戏开始:");
System.out.println("猜字次数为7次");
int result = birthRandom();
//System.out.println(result);
for (int i = 1; i < 7; i++) {
int guessNumber = birthNumber();
arr.add(guessNumber);
if(result == guessNumber) {
System.out.println("恭喜您猜对了...");
break;
}else if(result < guessNumber){
System.out.println("猜大了,请重新猜...");
}else{
System.out.println("猜小了,请重新猜...");
}
}
System.out.println("游戏结束");
System.out.println("您猜过的数字如下:");
System.out.println(arr);
System.out.println("是否继续游戏?");
System.out.println("1 是 2 否");
int i = sc.nextInt();
if(i==1) {
guessNumber();
}
}
//生成随机数
public static int birthRandom() {
System.out.println("请选择游戏难度:");
System.out.println("1 简单(0-50) 2 普通(0-100) 3 困难(0-200)");
int num = sc.nextInt();
int i = 0;
switch(num) {
case 1 :
i = 51;
break;
case 2 :
i = 101;
break;
case 3 :
i = 201;
break;
}
Random r = new Random();
int result = r.nextInt(i);
return result;
}
//键盘录入一个数字
public static int birthNumber() {
System.out.println("请输入您要猜的数字:");
int guessNumber = sc.nextInt();
return guessNumber;
}
}
|
|