Java 高亮显示和选择输入
我开始用java编写一个类似于武器店的游戏。我试图让用户能够选择控制台中 HashMap 中打印的项目。一旦用户选择并突出显示该商品,用户就会购买该商品。 Java 有内置实用程序支持此功能吗?这是我所拥有的:
public static int purchaseMedicine(int goldAmount) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("Jewel of Open", 500);
map.put("Potion", 800);
map.put("Hi-Potion", 2000);
map.put("Elixir", 8000);
map.put("Manna Prism", 4000);
map.put("Antivenom", 200);
map.put("Hammer", 200);
map.put("Library Card", 500);
int index = 0;
for(Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
System.out.printf("\n\tGold %d\n\n", goldAmount);
return index; // Returns the instances of medicineList
}
I've started writing a game in java similar to a weapons shop. I'm trying to have the user have the ability to select items printed in a HashMap within the console. Once the user selects and highlights the item, the user would've bought that item. Is there a built in utility Java has that supports this? Here's what I have:
public static int purchaseMedicine(int goldAmount) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("Jewel of Open", 500);
map.put("Potion", 800);
map.put("Hi-Potion", 2000);
map.put("Elixir", 8000);
map.put("Manna Prism", 4000);
map.put("Antivenom", 200);
map.put("Hammer", 200);
map.put("Library Card", 500);
int index = 0;
for(Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
System.out.printf("\n\tGold %d\n\n", goldAmount);
return index; // Returns the instances of medicineList
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
内置于 Java 中,答案是否定的。
可在标准 Java 中使用,可以尝试使用
System.out.println 中的 ANSI 转义码 ()
语句,但这些语句可能会被您正在使用的操作系统控制台破坏。如果您愿意导入一个小 jar 文件,最好的解决方案是 Jansi。在这里,程序员可以更好地控制控制台中显示的内容。所以基本上代码看起来像这样:
您只需要自己进行逻辑检查即可查看该商品是否已被购买。
Built into Java the answer is no.
Usable from standard Java, one can try ANSI escape codes from say
System.out.println()
statements, but these could be mangled by the OS console you are using.The best solution, if you are willing to import one tiny jar file, is Jansi. Here, the programmer has better control over what appears in the console. So basically the code can look something like this:
You just need to put in the logical check yourself to see if the item has already been bought.