我的代码以我不想要的方式重复

时间:2021-08-30 21:16:44

At the end of my code when the user chooses to repeat. When it does it outputs the whole loop with the original work instead of a new inputted word. For example if the user inputs the word Pizza. It will do the loop the way I that I want it to. When I choose to repeat it, the loop will use the word Pizza again instead of asking me for a different word.

在我的代码结束时,用户选择重复。当它这样做时,它输出整个循环与原始工作而不是新输入的单词。例如,如果用户输入单词Pizza。它会像我想要的那样循环。当我选择重复它时,循环将再次使用单词Pizza而不是向我询问另一个单词。

do {
 if (answer == 1){
    System.out.println("Please enter another word:");
}
    for (int i = 0; i < word.length(); i++) {
    firstLetter = word.charAt(0);
    word = word.substring(1, word.length());
    System.out.println(firstLetter + word);
    word += firstLetter;

}
 System.out.println("Would you like to Enter another word? If so, press 1. If not press 2.");
answer = input.nextInt();
input.nextLine();
}
    while(answer == 1);
    System.out.println("Have a nice day!");
}
}

The output for this code is: pizza izzap zzapi zapiz apizz Would you like to Enter another word? If so, press 1. If not press 2. When I press 1 it will repeat it instantly without asking me for a new word. How can I get the desired result?

此代码的输出是:pizza izzap zzapi zapiz apizz您想输入另一个单词吗?如果是这样,请按1.如果不按2.当我按1时,它会立即重复,而不会要求我换新词。我怎样才能得到理想的结果?

2 个解决方案

#1


2  

System.out.println("Would you like to Enter another word? If so, press 1. If not press 2.");
answer = input.nextInt();
input.nextLine();

I think if you take out the last line it will work properly

我想如果你拿出最后一行它会正常工作

Updated

#2


1  

Maybe something like this instead. You are missing the input for the word as well. You need both the word and the action from the user.

也许这样的事情。您也缺少该单词的输入。您需要来自用户的单词和操作。

do {
    System.out.println("1 for new word, 2 for exit");
    answer = input.nextInt();
    input.nextLine();
    System.out.println("input word");
    String word = input.nextLine();

    if (answer == 1){   
        for (int i = 0; i < word.length(); i++) {
            firstLetter = word.charAt(0);
            word = word.substring(1, word.length());
            System.out.println(firstLetter + word);
            word += firstLetter;
        }
    }  
} while(answer == 1);
System.out.println("Have a nice day!");

#1


2  

System.out.println("Would you like to Enter another word? If so, press 1. If not press 2.");
answer = input.nextInt();
input.nextLine();

I think if you take out the last line it will work properly

我想如果你拿出最后一行它会正常工作

Updated

#2


1  

Maybe something like this instead. You are missing the input for the word as well. You need both the word and the action from the user.

也许这样的事情。您也缺少该单词的输入。您需要来自用户的单词和操作。

do {
    System.out.println("1 for new word, 2 for exit");
    answer = input.nextInt();
    input.nextLine();
    System.out.println("input word");
    String word = input.nextLine();

    if (answer == 1){   
        for (int i = 0; i < word.length(); i++) {
            firstLetter = word.charAt(0);
            word = word.substring(1, word.length());
            System.out.println(firstLetter + word);
            word += firstLetter;
        }
    }  
} while(answer == 1);
System.out.println("Have a nice day!");