Почему это не работает, когда я ввожу numOfTimes, код перестает работать? (Генератор имен)

avatar
PecoAnnArbor
7 апреля 2018 в 22:02
32
1
0

Я новичок в 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++;
    }

}

Первое изображение — это код, где numOfTimes = 1, второе изображение — это нерабочий код, где numOfTimes > 1

Источник

Ответы (1)

avatar
mszymborski
7 апреля 2018 в 22:04
1

Это не работает, поскольку условие j == numOfTimes ложно, поэтому в цикле фактически ничего не выполняется. Измените его на j <= numOfTimes.

PecoAnnArbor
7 апреля 2018 в 22:05
0

Итак, в основном условие цикла while должно быть истинным, а затем вам нужно увеличить его до ложного, чтобы завершить его? Я прав?

mszymborski
7 апреля 2018 в 22:05
0

@PecoAnnArbor: да