While循环不循环布尔java



我对Java编码非常陌生,我已经在这个问题上工作了大约4天。我在编一个你自己选择的结局故事。当我到达第69行时,如果读者选择是,我希望系统重新开始故事,如果他们选择否,则结束程序。我不确定我是否正确地编写了while循环或者我是否可以使用break;就像我在使用它一样。抱歉我的代码太丑了,哈哈,我正在努力做得更好。任何建议都是感激的!

package journey;
import java.util.Scanner;
import java.lang.String;
public class journey {

public static void main(String[] args) {

// TODO Auto-generated method stub
Scanner decision = new Scanner (System.in); 
//Scanner play = new Scanner (System.in);
String selection;
Boolean no = false;
Boolean yes = true;
//end.equals(end);

while (yes.equals(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?");
//Maybe we can insert a car ride scene here to town, he arrives at the apothecary shop and goes in
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':  ");
yes = decision.nextBoolean();   
if (yes.equals(true)) { return;
} //else { 
//System.out.println("Goodbye"); return;}
yes.equals(true);} // end while loop 

} // class body

}

https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#hasNextBoolean()

Scanner.nextBoolean()期望TrueFalse(不区分大小写),而不是Yes/No

public static void main(String[] args) {
Scanner s = new Scanner("yes");
//s.nextBoolean() throws an InputMismatchException
System.out.println(s.nextBoolean());
}

最新更新