简体   繁体   中英

While loop not looping with boolean java

I'm very new to coding in Java and I have been working on this problem for about 4 days. I'm making a choose your own ending story. When I get to line 69, I would like the system to start the story over if the reader selects yes and to end the program if they select no. I'm not sure if I'm coding the while loop correctly or if I can even use the break; operator like I am using it. Sorry my code is so ugly, lol, I'm working hard on getting better. Any advice is appreciated!

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() expects True or False (case insensitive), not Yes/No

public static void main(String[] args) {
    Scanner s = new Scanner("yes");

    //s.nextBoolean() throws an InputMismatchException
    System.out.println(s.nextBoolean());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM