代码拉取完成,页面将自动刷新
同步操作将从 筱泷澄汐/xlcx01 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
public class GuessNumber {
public static Scanner scanner = new Scanner(System.in);
public static ArrayList<Integer> answers = new ArrayList<>();
public static ArrayList<Integer> guesses = new ArrayList<>();
public static void main(String[] args) {
scanner = new Scanner(System.in);
System.out.println("欢迎来到新猜数字游戏!");
while (true) {
answers = generateAnswer();
while (true) {
System.out.println("请输⼊4个0-9之间不重复的整数:");
guesses.clear();
guesses = receiveGuesses();
int a = getA();
int b = getB();
System.out.println(a + "A" + b + "B");
if (a == 4 && b == 0) {
System.out.println("恭喜你!猜对啦!");
break;
}
}
System.out.println("再玩⼉⼀次吗?(y/n)");
String again = scanner.next();
if (!again.equalsIgnoreCase("y")) {
break;
}
}
}
// ⽣成答案数字⽅法
public static ArrayList<Integer> generateAnswer() {
while (answers.size() < 4) {
int answer = (int) (Math.random() * 10);
if (!answers.contains(answer)) {
answers.add(answer);
}
}
return answers;
}
// 接受猜测数字⽅法
public static ArrayList<Integer> receiveGuesses() {
while (guesses.size() < 4) {
int guess;
try {
guess = scanner.nextInt();
} catch (InputMismatchException e) {
System.out.println("请输⼊数字!");
scanner.next();
continue;
}
if (guess < 0 || guess > 9) {
System.out.println("请输⼊0-9之间的整数!");
continue;
}
if (!guesses.contains(guess)) {
guesses.add(guess);
} else {
System.out.println("数字已经存在!");
}
}
return guesses;
}
// 统计猜测数字中A类数字个数的⽅法
public static int getA() {
int a = 0;
for (int i = 0; i < 4; i++) {
if (answers.get(i) == guesses.get(i)) {
a = a + 1;
}
}
return a;
}
// 统计猜测数字中B类数字个数的⽅法
public static int getB() {
int b = 0;
for (int i = 0; i < 4; i++) {
if (answers.contains(guesses.get(i)) && answers.get(i) !=
guesses.get(i)) {
b = b + 1;
}
}
return b;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。