我是编程初学者(学习 Java)。我正在尝试编写一个程序,其中列出了四个不同的选项供用户选择。
这是其中的一部分:
import java.util.*;
public class fight {
public static int upgrade1 = 0;
public static int upgrade2 = 0;
public static int upgrade3 = 0;
public static int upgrade4 = 0;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter your name:");
String player = scan.next();
System.out.println("You have earned 2 upgrade points. Which of the following traits would you like to boost by 2 points?\n"
+ " 1. upgrade1\n 2. upgrade2\n 3. upgrade3\n"
+ " 4. upgrade4");
if (scan.nextInt() == 1) {
upgrade1 = upgrade1 + 2;
System.out.println("Your upgrade1 level is now: " + upgrade1);
}
else if (scan.nextInt() == 2) {
upgrade2 = upgrade2 + 2;
System.out.println("Your upgrade2 level is now: " + upgrade2);
}
else if (scan.nextInt() == 3) {
upgrade3 = upgrade3 + 2;
System.out.println("Your upgrade3 level is now: " + upgrade3);
}
else if (scan.nextInt() == 4) {
upgrade4 = upgrade4 + 2;
System.out.println("Your upgrade4 level is now: " + upgrade4);
}
}
}
问题是:当用户输入他们想要选择的选项时,他们必须输入数字(x 是他们选择的数字)x 次。例如,用户想要选择选项 3。他们必须在控制台中输入数字 3 三次,然后控制台才能理解并完成下一行。
这是运行程序后的控制台:
请输入您的姓名:rick 你好,rick。您已获得 2 个升级点。您希望将以下哪个特质提升 2 点?1. upgrade1 2. upgrade2 3. upgrade3 4. upgrade4 3 3 3 你现在的 upgrade3 等级是:2
我希望这是有道理的,并且非常感谢任何帮助(我假设我只是在犯一个愚蠢的新手错误)。此外,如果您对它的结构方式有任何建设性的批评,请不要犹豫。谢谢!