java - 如何回调方法中的特定代码行?
问题描述
我是编程新手,正在编写我的第一个游戏。我遇到的一个问题是我不知道如何跳转到不是方法的特定行或代码段。
System.out.println("\n--------------------------------------------\n");
System.out.println("Hello customer, what would you like to do today?");
System.out.println("1: Buy");
System.out.println("2: Sell");
System.out.println("3: Leave");
System.out.println("\n--------------------------------------------\n");
choice = myScanner.nextInt();
if (choice==1) {
System.out.println("\n--------------------------------------------\n");
System.out.println("What would you like to buy?");
System.out.println("1: Chain Armor-500 Gold.");
System.out.println("2: Gold Broadsword-200 Gold.");
System.out.println("3: Nevermind.");
System.out.println("\n--------------------------------------------\n");
choice = myScanner.nextInt();
if (choice==1) {
}
else if (choice==2) {
}
else if (choice==3) {
} else {
}
我想跳到运行 else 语句时询问他们想购买什么的部分。
解决方案
正如@JohannesKuhn 提到的,这可以使用while
循环来实现:
if (choice==1) {
while(true) {
System.out.println("\n--------------------------------------------\n");
System.out.println("What would you like to buy?");
System.out.println("1: Chain Armor-500 Gold.");
System.out.println("2: Gold Broadsword-200 Gold.");
System.out.println("3: Nevermind.");
System.out.println("\n--------------------------------------------\n");
choice = myScanner.nextInt();
if (choice==1) {
} else if (choice==2) {
} else if (choice==3) {
} else {
continue; // Skips the rest of the code, returning to the start of the loop.
}
break; // Breaks out of the loop, "placing" you after it's closing bracket
}
}
推荐阅读
- python - 如何根据另一个键获取元素的键
- python - 试图从 webelements 列表中提取文本,但我只得到第一个
- forms - Vue.js 重用表单验证规则
- ssl - 解密由于 JMeter https 请求(例如通过 Wireshark)生成的“加密警报”数据包
- dockerfile - 为什么运行时会出现 chown:在 `/usr/local/hadoop/logs' 之后缺少操作数
- git - 有没有办法从数据目录中恢复 gitlab 存储库,如下所示?
- node.js - 过程或函数 insert_azienda 指定了太多参数 (Node.js)
- hyperledger-fabric - 如何创建频道更新提案以及如何签名
- android - 如何执行方法onClick?
- jwt - 使用 java 库 auth0-java 为 JitsiMeet 生成 jwt 令牌