Domanda

I am having trouble with stopping this program.

È stato utile?

Soluzione

The issue with the code is that you set your index inside of the loop. There is absolutely no reason why you should be doing this here. It is usually the case with for loops that you don't want to modify the index inside of the loop.

For reference the code should look like:

    for (int i = 0; i < word.length(); i++) {
        firstLetter = word.charAt(i);
        word = word.substring(1, word.length());
        System.out.println(firstLetter + word);

        word += firstLetter;
    }

This will at least ensure that your loop will end, but it won't necessarily give you the desired output. Since your question was specific to ending the loop and since this sounds more like a homework assignment, I'll let you do the debugging to make sure you're getting the correct output.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top