Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在程序中输出用户选择的选项?_Java - Fatal编程技术网

Java 如何在程序中输出用户选择的选项?

Java 如何在程序中输出用户选择的选项?,java,Java,我的程序将向用户展示一个框,并为他们提供多个选项。因此,他们必须输入一个整数,该整数表示他们希望进行的步骤。我想知道如何输出他们在程序中选择的步骤。我需要为此使用for语句吗 可以是for循环,也可以将选项文本存储在地图中,通过按键选择索引 如果将它们放入LinkedHashMap,也可以在for循环中打印它们,而不是手动打印每个选项 Map<Integer, String> options = new LinkedHashMap<>(); options.put(1,

我的程序将向用户展示一个框,并为他们提供多个选项。因此,他们必须输入一个整数,该整数表示他们希望进行的步骤。我想知道如何输出他们在程序中选择的步骤。我需要为此使用for语句吗


可以是
for
循环,也可以将选项文本存储在
地图
中,通过按键选择索引

如果将它们放入
LinkedHashMap
,也可以在
for
循环中打印它们,而不是手动打印每个选项

Map<Integer, String> options = new LinkedHashMap<>();
options.put(1, "BTEC 90 Credit Diploma Grade");
...

for (Entry<Integer, String> entry : options.entrySet()) {
    System.out.println(
        "| |" + entry.getKey().toString() + "|         "
        + entry.getValue() + repeat(" ", 40 - entry.getValue().length())
        + "|"
    );
}

...

Integer choice = input.nextInt();
System.out.println("Answer entered was " + options.get(choice));
Map options=newlinkedhashmap();
期权。看跌期权(1,“BTEC 90学分文凭等级”);
...
for(条目:options.entrySet()){
System.out.println(
“||”+entry.getKey().toString()+”|”
+entry.getValue()+repeat(“,40-entry.getValue().length())
+ "|"
);
}
...
整数选择=input.nextInt();
System.out.println(“输入的答案是”+options.get(choice));
就可以了

要获得号码,只需执行以下操作:

int num = input.nextInt();
一旦你有了号码,你就可以用了

if (num == 1) { System.out.println(option1); }

选择一。继续你所有的选择。不需要循环,因为您知道每个选项对应的编号。

您想打印什么?他们选择的整数,或关联的名称?请指定关联的名称
Map<Integer, String> options = new LinkedHashMap<>();
options.put(1, "BTEC 90 Credit Diploma Grade");
...

for (Entry<Integer, String> entry : options.entrySet()) {
    System.out.println(
        "| |" + entry.getKey().toString() + "|         "
        + entry.getValue() + repeat(" ", 40 - entry.getValue().length())
        + "|"
    );
}

...

Integer choice = input.nextInt();
System.out.println("Answer entered was " + options.get(choice));