2

我是编程初学者(学习 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

我希望这是有道理的,并且非常感谢任何帮助(我假设我只是在犯一个愚蠢的新手错误)。此外,如果您对它的结构方式有任何建设性的批评,请不要犹豫。谢谢!

4

4 回答 4

2

您不能重复调用 scan.nextInt()。当然,除非您期望读取多个不同的整数。

反而:

Scanner scan = new Scanner(System.in);
System.out.println("Please enter your name:");
String player = scan.next();
int ichoice = scan.nextInt();
switch (ichoice) {
  case 1:
  ...
于 2012-10-08T14:58:59.450 回答
1

每次调用scan.nextInt一个 if 语句时,它都会读取另一个 int。改成:

int userChoice = scan.nextInt();
if (userChoice == 1)
{
    ...
}
else if (userChoice == 2)

...

至于建设性的批评,选择你喜欢的风格并使用它。你的缩进到处都是;这使代码更难阅读。它是否是一种常用的风格并不重要,也不管其他人如何看待它,只要确保喜欢它并坚持下去。

Eclipse 可以为您自动格式化代码,并且这种行为是可定制的(您可以对其进行调整,使其与您的风格相匹配)。

于 2012-10-08T14:58:35.300 回答
0

每次调用时nextInt(),都会从输入中读取另一个 int。因此,您只想调用它一次!

int choice = scan.nextInt();

if (choice == 1) ...
if (choice == 2) ...
于 2012-10-08T14:58:33.590 回答
0

这是因为您在每个 if/else if 语句中都调用了 scan.nextInt()。您要做的是存储他们输入的值,然后检查 if/else if 语句中的值,否则您基本上是在多次提示用户输入。

int input = scan.nextInt();

if (input == 1) {
    upgrade1 = upgrade1 + 2;
        System.out.println("Your upgrade1 level is now: " + upgrade1);
}
else if (input == 2) {
    upgrade2 = upgrade2 + 2;
        System.out.println("Your upgrade2 level is now: " + upgrade2);
}
else if (input == 3) {
    upgrade3 = upgrade3 + 2;
        System.out.println("Your upgrade3 level is now: " + upgrade3);
}
else if (input == 4) {
    upgrade4 = upgrade4 + 2;
        System.out.println("Your upgrade4 level is now: " + upgrade4);
}
于 2012-10-08T15:00:44.533 回答