当前位置: 首页 > 知识库问答 >
问题:

而不是使用布尔java循环

戴浩初
2023-03-14

我对Java编码非常陌生,我已经研究这个问题大约4天了。我正在做一个选择你自己的结局故事。当我到达第69行时,如果读者选择是,我希望系统重新开始故事,如果他们选择否,则结束程序。我不确定我是否正确地编码了while循环,或者我甚至可以使用中断;操作员就像我在使用它一样。对不起,我的代码太难看了,哈哈,我正在努力变得更好。感谢您的建议!

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

}

共有1个答案

洪楚
2023-03-14

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

Scanner.nextBoolean()需要TrueFalse(不区分大小写),而不是是/否

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

    //s.nextBoolean() throws an InputMismatchException
    System.out.println(s.nextBoolean());
}
 类似资料:
  • 我遇到了一个我还没有遇到的问题。我用java写了一个猜谜游戏来猜1到1000之间的数字。程序应该循环,直到玩家决定不再玩为止。然而,我无法让它循环。如果我取出程序底部的Scanner类,它可以正常循环,但我需要Scanner类按照类中的要求完成程序。如果用户在scanner类中键入“no”,则while循环变量设置为false,程序将按其应该的方式终止。但是,如果用户选择“yes”并且while循

  • 问题内容: 我已经看过SO中的其他volatile与Atomicxxxx问题(包括此问题),并阅读了java.util.current.atomic的描述,但我对细微差别并不满意。 如果我要决定在和之间使用,除了AtomicBoolean提供的原子读取-修改-写入操作之外,还有其他实际区别吗?(例如和) 假设我有 然后,一个或多个线程设置该标志(但不清除它)。如果我有一个线程读取该标志,并且设置了

  • 问题内容: 我也在学习Java和android。我们可以在while循环中执行的几乎所有事情都可以在while循环中执行。 我发现一个简单的条件,使用while循环比for循环更好 如果我必须在程序中使用counter的值,那么我认为while循环要比for循环好 使用while循环 在这种情况下,我发现while循环要比for循环好,因为如果要在for循环中实现相同的效果,则必须将counter

  • 我注意到java有一个问题。它不能解析空值的布尔类。我知道它有静态方法,但它的签名声明它只接受,而不接受。 换句话说,它有以下签名: 但不是: 检查布尔值而不陷入NullPointerException的最佳方法是什么?

  • 首先,如果我搞砸了,很抱歉,这是我在这里的第一个问题。第二,对不起,我的代码很乱,我刚开始在学校学习java。 所以,我编写了一个计时器,从00:00:00上升到23:59:59。秒、分钟和小时使用内部和外部循环上升,然后每秒钟在控制台中打印一次。我有一个布尔值来检查值是否小于10。如果数字小于10,它会在数字前加上0,这样它就被打印为01:02:03而不是1:2:3。问题是,一旦值等于或大于10

  • 问题内容: 我正在将项目从JAXB 1.0迁移到JAXB 2.1,并且数据类型映射存在问题。 我正在使用Ant xjc 绑定编译器,并且已经成功配置了全局绑定,例如(例如) xs:date 映射到 java.util.Calendar 。 但是我得到了生成的方法,这些方法可以返回,而我却想要。 这是复杂的类型: 生成的类如下所示: 问题是,尽管装箱可以工作,但是如果提供的XML不包含的值,则该类的