通过这段代码起到练习循环和条件语句的作用。
package zjw.loop;
import java.util.Random;
import java.util.Scanner;
public class RandomDemo_exp {
public static void main(String[] args) {
Choose();
}
public static String menu() {
System.out.println("-----------------------");
System.out.println("- Y.开始游戏 N.退出游戏 -");
System.out.println("-----------------------");
System.out.print("请选择是否开始游戏:");
Scanner scanner = new Scanner(System.in);
return scanner.next();
}
public static void Choose() {
while (true) {
String choose = menu();
if (choose.equals("Y") || choose.equals("y")) {
guess();
} else if (choose.equals("N") || choose.equals("n")) {
System.out.println("游戏退出");
break;
} else {
System.out.println("输入错误");
}
}
}
public static void guess() {
Random random = new Random();
Scanner scanner = new Scanner(System.in);
int time;
int bound;
System.out.println("-----------------------");
System.out.println("- 1.难度一 2.难度二 -");
System.out.println("-----------------------");
System.out.print("请选择游戏难度(1 or 2 ):");
int time_Number = scanner.nextInt();
switch (time_Number) {
case 1 -> {
bound = 100;
time = 10;
}
case 2 -> {
bound = 100;
time = 5;
}
default -> {
bound = 100;
time = 999;
}
}
int trueNumber = random.nextInt(bound) + 1;
int num = 0;
System.out.println(trueNumber);//直接输出答案便于测试
while (time > 0) {
System.out.print("请输入一个1~100之间的数:");
int guessNumber = scanner.nextInt();
if (guessNumber > trueNumber) {
System.out.println("测得数大了");
} else if (guessNumber < trueNumber) {
System.out.println("测得数小了");
} else {
System.out.println("恭喜你猜中了,答案是:" + trueNumber + " " + "共计猜测" + (num + 1) + "次");
System.out.println("剩余" + (time - 1) + "次!");
break;
}
/*
num++为下列程序的循环次数 也就是测错部分的次数 num+1在else{}内执行意味:当猜测正确时,所有猜错的次数加上这次猜对的次数即为猜测次数的总数
if () {
} else if () {
}
*/
num++;
time--;
System.out.println("剩余" + time + "次");
}
}
}
menu()的作用:
public static String menu() {
System.out.println("-----------------------");
System.out.println("- Y.开始游戏 N.退出游戏 -");
System.out.println("-----------------------");
System.out.print("请选择是否开始游戏:");
Scanner scanner = new Scanner(System.in);
return scanner.next();
}
提供一个简单的界面样式(大雾),为choose()提供一个参数。
choose()的作用:
public static void Choose() {
while (true) {
String choose = menu();
if (choose.equals("Y") || choose.equals("y")) {
guess();
} else if (choose.equals("N") || choose.equals("n")) {
System.out.println("游戏退出");
break;
} else {
System.out.println("输入错误");
}
}
}
通过 String choose = menu()获取 menu()传递过来的参数,进行下一步的判断,当键入值为("Y"||"y")时调用guess(),当键入值为("N"||"n")时退出程序,当键入值为其他时提示重新输入。
guess()的作用
public static void guess() {
Random random = new Random();
Scanner scanner = new Scanner(System.in);
int time;
int bound;
System.out.println("-----------------------");
System.out.println("- 1.难度一 2.难度二 -");
System.out.println("-----------------------");
System.out.print("请选择游戏难度(1 or 2 ):");
int time_Number = scanner.nextInt();
switch (time_Number) {
case 1 -> {
bound = 100;
time = 10;
}
case 2 -> {
bound = 100;
time = 5;
}
default -> {
bound = 100;
time = 999;
}
}
int trueNumber = random.nextInt(bound) + 1;
int num = 0;
System.out.println(trueNumber);//直接输出答案便于测试
while (time > 0) {
System.out.print("请输入一个1~100之间的数:");
int guessNumber = scanner.nextInt();
if (guessNumber > trueNumber) {
System.out.println("测得数大了");
} else if (guessNumber < trueNumber) {
System.out.println("测得数小了");
} else {
System.out.println("恭喜你猜中了,答案是:" + trueNumber + " " + "共计猜测" + (num + 1) + "次");
System.out.println("剩余" + (time - 1) + "次!");
break;
}
/*
num++为下列程序的循环次数 也就是测错部分的次数 num+1在else{}内执行意味:当猜测正确时,所有猜错的次数加上这次猜对的次数即为猜测次数的总数
if () {
} else if () {
}
*/
num++;
time--;
System.out.println("剩余" + time + "次");
}
}
这段代码的核心内容,time为游戏一共可用的猜测次数,bound为猜测的数据范围,time_Number为游戏的难度(通过键入获取),num为已经猜测的次数
switch语句的作用是选择游戏难度,并且每次继续游戏都能提供新的答案(格式可能为JAVA18才有的)
switch (time_Number) {
case 1 -> {
bound = 100;
time = 10;
}
case 2 -> {
bound = 100;
time = 5;
}
default -> {
bound = 100;
time = 999;
}
}
接下来的while语句最简单的猜随机数游戏的代码稍加修改后的样子。