我是一个非常新的程序员,我刚刚学习了扫描仪,但是我试图找到一种方法来实现它,所以当我按下向上或向下键时,它会循环选项,放一个>在前面知道它是当前选择的选项。
这是提示:
What would you like to do:
Attack
Inventory
Stats
Flee
我希望在所选的前面有一个箭头,并使用向上和向下箭头键更改选择。例如,一旦提示您,它就像这样:
您想做什么:
What would you like to do:
> Attack
Inventory
Stats
Flee
如果按下向下箭头键两次:
What would you like to do:
Attack
Inventory
> Stats
Flee
所以我目前的代码如下:
public class Prompter {
Scanner input = new Scanner(System.in);
public int options() throws IOException {
System.out.printf("What would you like to do:%nAttack %n Inventory %n Stats %n Flee %n");
while (!input.hasNextInt()) input.next();
int choice = input.nextInt();
System.out.printf("Choice: %d", choice);
return choice;
}
}
它现在所做的是接受一个int输入并返回它。
请不要向我发送大量代码而不解释它,因为我是一个非常新的程序员。谢谢!
答案 0 :(得分:1)
正如之前的评论所述,无法在控制台中轻松实现此类行为。
一个简单的解决方法可能是在选项后面映射数字。 (Attack(1),Inventory(2),..)并扩展您的代码,如:
int inputChoice = input.nextInt();
String choice = "";
switch(inputChoice){
case 1: choice = "Action";
case 2: choice = "Inventory";
// add cases
}
System.out.println("Choice: " +choice);
return choice; // or inputChoice if you want to return an int value
这不是最好的方法,但可能足以满足您当前的需求。