字符串循环Java

字符串循环Java,java,while-loop,Java,While Loop,将输入提供给Writenbooks后,它将跳过为authorDemographics输入的选项,并继续执行下一个提示。此时,无论我输入了什么,它都会不断提示我“输入1表示是,输入2表示否”。期待任何反馈。谢谢 /* body */ System.out.println ("Hello, welcome to the convention!"); System.out.println ("Please enter your full name: "); auth

将输入提供给Writenbooks后,它将跳过为authorDemographics输入的选项,并继续执行下一个提示。此时,无论我输入了什么,它都会不断提示我“输入1表示是,输入2表示否”。期待任何反馈。谢谢

    /* body */
    System.out.println  ("Hello, welcome to the convention!");
    System.out.println  ("Please enter your full name: ");
    authorName = input.nextLine ();
    System.out.flush();
    System.out.println  ("Please enter the amount of books you have written: ");
    writtenBooks = input.nextInt ();
    System.out.println  ("Who is your target demographic for your books?");
    System.out.println  ("Type: Under 3 , 3 through 7 , 8 through 10 , 11 through 13 , or 14 and older.");
    authorDemographics = input.nextLine ();
    System.out.flush();      

    /* loop */

    demoLoop = 0;
    while (!ageGroup [4].equals(authorDemographics) && demoLoop == 0) {
        System.out.println  ("Would you like to enter more demographics?");
        System.out.println  ("Type 1 for Yes. Type 2 for No.");
        question = input.nextInt (); }


        if (question == 1) {
            System.out.println  ("Who is your target demographic for your books?");
            System.out.println  ("Type: Under 3, 3 through 7, 8 through 10, 11 through 13, or 14 and older.");
            authorDemographics01 = input.nextLine ();
            System.out.flush();
    } else {
        demoLoop = 1;    
        System.out.println  ("Author Name:" + authorName);
        System.out.println  ("First Demographic: " + authorDemographics);
        System.out.println  ("Second Demographic: " + authorDemographics01);
        System.out.println  ("Amount of books written: " + writtenBooks);

看看while循环的括号。您永远不会更改退出条件的任何操作数的值,因此如果第一次为真,它将自然无限循环。

我认为您的缩进和括号混淆了

例如,让我们只看一下while循环

while (!ageGroup [4].equals(authorDemographics) && demoLoop == 0) {
        System.out.println  ("Would you like to enter more demographics?");
        System.out.println  ("Type 1 for Yes. Type 2 for No.");
        question = input.nextInt (); }
您永远不会更改
ageGroup[4]
demoloop
,因此一旦循环结束,它将再次检查条件,并且条件将始终为真。(运动中的回路保持运动状态,除非对其采取行动)


您的if看起来也缩进了。这并不意味着它在你的循环中。循环结束时,匹配的括号与缩进无关。是否希望if和else在循环中?

是否尝试在调试器中单步执行代码?你到底有什么问题?顺便说一句,您不需要调用System.out.flush();在我看来,问题是
question=input.nextInt();}之后的右括号我不希望它跳过任何内容,我希望循环正确退出。我还没有调试它,但即使我调试了,我也不知道该怎么做。我对编码还是新手。/*声明*/int参与者=0;字符串[]年龄组;年龄组=新字符串[5];年龄组[0]=“3岁以下”;年龄组[1]=“3至7”;年龄组[2]=“8至10”;年龄组[3]=“11至13”;年龄组[4]=“14岁及以上”;字符串authorName=“”;int-num;国际书写书籍;int num1;字符串authorDemographics=null;字符串authorDemographics01=null;整数问题=0;int-demoLoop;我的蜘蛛感知是说,
nextLine
不读入新行,这意味着新行随后被输入到
nextLine
,并且这一过程被重复了一遍又一遍。