Java中布尔值控制while循环无法正常循环的问题求解
文字冒险游戏重玩逻辑修复方案
现有代码核心问题
- 循环控制逻辑完全写反:当前代码判断用户输入为
true(对应预期的yes输入)时直接执行return,会直接终止整个程序,和「输入yes重开、输入no退出」的需求完全相反。 - 输入类型不匹配:提示用户输入
yes/no字符串,却调用decision.nextBoolean()读取输入,用户输入字符串时会直接抛出输入不匹配异常,程序直接崩溃。 - 输入缓冲区残留bug:混用
nextBoolean()/next()和nextLine()方法时,前一个方法会把换行符留在输入缓冲区,导致后续读取剧情选项时会直接跳过用户输入步骤,出现选完一个选项直接跳后续剧情的问题。 - 剧情分支断层:选择
root的结局触发后,代码不会停,还会继续往下执行drink/toss的选择逻辑,不符合剧情设计。 - 冗余无效代码:定义了从未使用的
Boolean no = false变量,循环末尾单独写的yes.equals(true);没有任何赋值或逻辑判断作用,属于无效代码。
流程控制语句使用规范
return:在main方法中执行return会直接终止整个程序运行,适合用户选择退出时调用,也可以通过修改循环条件让循环自然结束,可读性更好。break:作用是跳出当前所在的循环或switch分支,如果用while(true)写无限循环承载故事流程,用户选择不重玩时用break跳出循环,程序走到main方法末尾自然退出,逻辑更清晰。
修正后可直接运行的代码
package journey; import java.util.Scanner; public class journey { public static void main(String[] args) { Scanner decision = new Scanner(System.in); String selection; // 用无限循环承载故事流程,触发退出条件时再跳出 while (true) { System.out.println("A Journey Through Rem Forest"); System.out.println("Main character Justin suffers from chronic insomnia and is plagued by scarily vivid and lucid dreams when he does finally fall asleep. " + "We open the story with him in his bedroom, " + "sitting at his computer writing in his journal."); System.out.println("Journal entry “I haven’t slept in over 48 hours. It’s not like the measly hour or so I was getting before this bout was much help either….." + "I know I said I would not look on Webmd, but I couldn’t help myself. The longest recorded case of someone not sleeping was 11 days. Dear God, I can’t " + "imagine this going on for that long! I do not know what I would do!"); System.out.println(); System.out.println(); System.out.println("He shuts down his computer and hops in bed for the night. *he lays in bed staring at the ceiling, when morning arrives he in the same position*"); System.out.println(); System.out.println(); System.out.println("Justin knew he couldn’t last another night like this his aunt had been telling him about a new apothecary store in town that could probably help " + "with a holistic remedy. He decides to visit, what does he have to lose?"); System.out.println("Justin enters the shop, he is amazed at all the glass tubes with names of roots and herbs he had never heard of before. He walked along the shelves admiring the delicate glass vials, " + "the stone mortar and pestle’s on display. He was looking at a recipe book when he hear someone clear their throat behind him. He turns to see the tiniest grey haired woman he had ever seen, " + "she could not have been more than about 4’5 and was made even shorter by the way she hunched over her wooden cane.\n" + "'Name's Dorthy, may I help you?' She looked up at him with slight amusement on her face." + "Justin: I haven’t been sleeping, it’s been quite a while. I’m looking for something to help. I’ve tried a few over the counter items like melatonin and what not. Nothing has worked. My current record is 72 hours" + "Dorothy: (creepy laugh) well I might have just the thing to help you. I have two options for you. The first is a tried and true method, good ole valerian root. It’s locally sourced, carefully selected for potency. " + "The second is a new herb I just brought back with me from a near by town. The name of the plant escapes me, Colstea I believe?"); System.out.println("What will you choose? Type 'root' or 'herb': "); selection = decision.nextLine(); if (selection.equals("root")) { System.out.println("Justin continues to not sleep, but he is now the record holder for not sleeping 15 days, he made it into the book of world records and a couple of medical journals//player dies, game ends"); } else { System.out.println("Dorthy provides instructions on how to take the herb and to make sure to measure precisely, drink all that is made, recite the incantation and barricade all exits."); System.out.println(); System.out.println(); System.out.println("Justin questions if he should use the unknown herb or just make a doctor's appointment. He really didn’t want to have a bill if it was not necessary."); System.out.println(); System.out.println(); System.out.println("What will you do? Type 'toss' or 'drink'"); selection = decision.nextLine(); if (selection.equals("drink")) { System.out.println("Justin follows the steps to take the herb, it tastes horrible. He goes to bed tossing and turning, he’s pouring sweat and he feels like he’s paralyzed. " + "Above his bed what looked to be like a wormhole was opening up"); } else { System.out.println("Justin tosses herb in the trash, his dog gets into the trash and eats the herb. It has the opposite effects of sleep on animals. " + "Justin and and now his dog continue to not sleep, they both make it into the book of world record twice. " + "One individually for human and canine and one non sleeping duo."); } } // 重玩判断逻辑 System.out.println("Would you like to play again? Type 'yes' or 'no': "); String playAgain = decision.nextLine().trim().toLowerCase(); if (playAgain.equals("no")) { System.out.println("Goodbye"); break; } // 输入yes时无需额外操作,循环自动回到开头重开故事 } decision.close(); } }
注:所有原剧情文本完全保留,只修正逻辑bug,额外增加了输入前后空格、大小写兼容,即使用户输入带空格、首字母大写的Yes/NO也能正常识别。
内容的提问来源于stack exchange,提问作者Jessica Willis
相关产品推荐
相关产品推荐

