我是Java的初学者,我对为什么我的代码无法正常工作有疑问。因此,基本上,当我将numOfTimes> 1设置为停止时。谁能告诉我原因以及如何解决?谢谢,如果有人帮助我,我将不胜感激!

package CodingSets;
import java.util.*;



import java.util.*;

public class nameGenerator {
public static void rules() {
    System.out.println("The rules arrr simple, give me the first
initial of your name, first intital of your middle name, and your last
initial");
}

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    char firstInitial;
    char middleInitial;
    char lastInitial;
    int numOfTimes = 0;
    System.out.print("How many times do you want to to enter your or your friends initials?\t");
    numOfTimes = scan.nextInt();
    int j = 1;
    while (j == numOfTimes) {

        String name = "";
        System.out.print("Enter your first initial : ");
        firstInitial = scan.next().charAt(0);
        scan = new Scanner(System.in);
        System.out.print("Enter your middle initial : ");
        middleInitial = scan.next().charAt(0);
        scan = new Scanner(System.in);
        System.out.print("Enter your last initial : ");
        lastInitial = scan.next().charAt(0);


            if(firstInitial == 'a' || firstInitial == 'A') {
                name += "Captain";

            } else if (firstInitial == 'b' || firstInitial == 'B') {
                name += "Dirty";

            } else if (firstInitial == 'c' || firstInitial == 'C') {
                name += "Squidlips";

            } else if (firstInitial == 'd' || firstInitial == 'D') {
                name += "Bowman";

            } else if (firstInitial == 'e' || firstInitial == 'E') {
                name += "Buccaneer";

            } else if (firstInitial == 'f' || firstInitial == 'F') {
                name += "Two Toes";

            } else if (firstInitial == 'g' || firstInitial == 'G') {
                name += "Sharkbait";

            } else if (firstInitial == 'h' || firstInitial == 'H') {
                name += "Old";

            } else if (firstInitial == 'i' || firstInitial == 'I') {
                name += "Peg Leg";

            } else if (firstInitial == 'j' || firstInitial == 'J') {
                name += "Fluffbucket";

            } else if (firstInitial == 'k' || firstInitial == 'K') {
                name += "Scallywag";

            } else if (firstInitial == 'l' || firstInitial == 'L') {
                name += "Bucko";

            } else if (firstInitial == 'm' || firstInitial == 'M') {
                name += "Dead Man";

            } else if (firstInitial == 'n' || firstInitial == 'N') {
                name += "Matey";

            } else if (firstInitial == 'o' || firstInitial == 'O') {
                name += "Jolly";

            } else if (firstInitial == 'p' || firstInitial == 'P') {
                name += "Stinky";

            } else if (firstInitial == 'q' || firstInitial == 'Q') {
                name += "Bloody";

            } else if (firstInitial == 'r' || firstInitial == 'R') {
                name += "Miss";

            } else if (firstInitial == 's' || firstInitial == 'S') {
                name += "Mad";

            } else if (firstInitial == 't' || firstInitial == 'T') {
                name += "Red";

            } else if (firstInitial == 'u' || firstInitial == 'U') {
                name += "Lady";

            } else if (firstInitial == 'v' || firstInitial == 'V') {
                name += "Bretheren";

            } else if (firstInitial == 'w' || firstInitial == 'W') {
                name += "Rapscallion";

            } else if (firstInitial == 'x' || firstInitial == 'X') {
                name += "Landlubber";

            } else if (firstInitial == 'y' || firstInitial == 'Y') {
                name += "Wrench";

            } else if (firstInitial == 'z' || firstInitial == 'Z') {
                name += "Freebooter";

            } else {
                System.out.println("Bruh learn ur alphebet");

            }



            if(middleInitial == 'a' || middleInitial == 'A') {
                name += " Creeper";

            } else if (middleInitial == 'b' || middleInitial == 'B') {
                name += " Jim";

            } else if (middleInitial == 'c' || middleInitial == 'C') {
                name += " Storm";

            } else if (middleInitial == 'd' || middleInitial == 'D') {
                name += " John";

            } else if (middleInitial == 'e' || middleInitial == 'E') {
                name += " George";

            } else if (middleInitial == 'f' || middleInitial == 'F') {
                name += " O'";

            } else if (middleInitial == 'g' || middleInitial == 'G') {
                name += " Rat";

            } else if (middleInitial == 'h' || middleInitial == 'H') {
                name += " Jack";

            } else if (middleInitial == 'i' || middleInitial == 'I') {
                name += " Legs";

            } else if (middleInitial == 'j' || middleInitial == 'J') {
                name += " Head";

            } else if (middleInitial == 'k' || middleInitial == 'K') {
                name += " Cackle";

            } else if (middleInitial == 'l' || middleInitial == 'L') {
                name += " Patch";

            } else if (middleInitial == 'm' || middleInitial == 'M') {
                name += " Bones";

            } else if (middleInitial == 'n' || middleInitial == 'N') {
                name += " Plank";

            } else if (middleInitial == 'o' || middleInitial == 'O') {
                name += " Greedy";

            } else if (middleInitial == 'p' || middleInitial == 'P') {
                name += " Sea";

            } else if (middleInitial == 'q' || middleInitial == 'Q') {
                name += " Mama";

            } else if (middleInitial == 'r' || middleInitial == 'R') {
                name += " Spike";

            } else if (middleInitial == 's' || middleInitial == 'S') {
                name += " Squiffy";

            } else if (middleInitial == 't' || middleInitial == 'T') {
                name += " Gold";

            } else if (middleInitial == 'u' || middleInitial == 'U') {
                name += " Yellow";

            } else if (middleInitial == 'v' || middleInitial == 'V') {
                name += " Felony";

            } else if (middleInitial == 'w' || middleInitial == 'W') {
                name += " Eddie";

            } else if (middleInitial == 'x' || middleInitial == 'X') {
                name += " Bay";

            } else if (middleInitial == 'y' || middleInitial == 'Y') {
                name += " Thomas";

            } else if (middleInitial == 'z' || middleInitial == 'Z') {
                name += " Spot";

            } else {
                System.out.println("Bruh learn ur alphebet");

            }

            if(lastInitial == 'a' || lastInitial == 'A') {
                name += " From the West";

            } else if (lastInitial == 'b' || lastInitial == 'B') {
                name += " Byrd";

            } else if (lastInitial == 'c' || lastInitial == 'C') {
                name += " Jackson";

            } else if (lastInitial == 'd' || lastInitial == 'D') {
                name += " Sparrow";

            } else if (lastInitial == 'e' || lastInitial == 'E') {
                name += " Of the Coast";

            } else if (lastInitial == 'f' || lastInitial == 'F') {
                name += " Jones";

            } else if (lastInitial == 'g' || lastInitial == 'G') {
                name += " Ned Head";

            } else if (lastInitial == 'h' || lastInitial == 'H') {
                name += " Bart";

            } else if (lastInitial == 'i' || lastInitial == 'I') {
                name += " O'Fish";

            } else if (lastInitial == 'j' || lastInitial == 'J') {
                name += " Kidd";

            } else if (lastInitial == 'k' || lastInitial == 'K') {
                name += " O'Malley";

            } else if (lastInitial == 'l' || lastInitial == 'L') {
                name += " Barnacle";

            } else if (lastInitial == 'm' || lastInitial == 'M') {
                name += " HolyStone";

            } else if (lastInitial == 'n' || lastInitial == 'N') {
                name += " Hornswaggle";

            } else if (lastInitial == 'o' || lastInitial == 'O') {
                name += " McStinky";

            } else if (lastInitial == 'p' || lastInitial == 'P') {
                name += " Swashbuckler";

            } else if (lastInitial == 'q' || lastInitial == 'Q') {
                name += " Sea Wolf";

            } else if (lastInitial == 'r' || lastInitial == 'R') {
                name += " Beard";

            } else if (lastInitial == 's' || lastInitial == 'S') {
                name += " Chumbucket";

            } else if (middleInitial == 't' || lastInitial == 'T') {
                name += " Rivers";

            } else if (middleInitial == 'u' || lastInitial == 'U') {
                name += " Morgan";

            } else if (lastInitial == 'v' || lastInitial == 'V') {
                name += " Tuna Breath";

            } else if (lastInitial == 'w' || lastInitial == 'W') {
                name += " Three Gates";

            } else if (lastInitial == 'x' || lastInitial == 'X') {
                name += " Bailey";

            } else if (lastInitial == 'y' || lastInitial == 'Y') {
                name += " Of Atlantis";

            } else if (lastInitial == 'z' || lastInitial == 'Z') {
                name += " Of Dark Water";

            } else {
                System.out.println("Bruh learn ur alphebet");

            }

        System.out.println("Your pirate name is " + name);
        j++;
    }

}


}

The First Image Is The Code Where numOfTimes = 1, The Second Image Is The Not Working Code Where numOfTimes > 1

最佳答案

由于条件j == numOfTimes为假,因此它不起作用,因此实际上没有执行循环中的任何操作。将其更改为j <= numOfTimes

09-13 05:43