循环不起作用,程序继续运行。

huangapple go评论78阅读模式
英文:

the loop is not working and the program just keep running

问题

public static void main(String[] args) {
    String action = null;
    Scanner scanner = new Scanner(System.in);
    action = scanner.nextLine();
    
    while (!"X".equals(action)){
        System.out.format("A) Enter block%nB) Determine number of spaces%nC) Swap 1st and last word%nD) Remove all vowels%nX) End%n");
        
        switch (action) {
            case "A":
                System.out.println("Please enter a sentence:");
                str1 = scanner.nextLine();
                break;

            case "B":
                if (str1 == null) {
                    System.out.println("No sentence entered yet!");
                } else {
                    System.out.println(SentenceInvestigator.countWhiteSpaces(str1) + " spaces found in sentence.");
                }
                break;

            case "C":
                if (str1 == null) {
                    System.out.println("No sentence entered yet!");
                } else {
                    String str2 = SentenceInvestigator.switchFirstAndLastWord(str1);
                    System.out.println(str2);
                }
                break;

            case "D":
                if (str1 == null) {
                    System.out.println("No sentence entered yet!");
                } else {
                    System.out.println(SentenceInvestigator.removeVowels(str1));
                }
                break;

            case "X":
                break;
        }
    }
}

Please note that the variables str1 and functions like SentenceInvestigator.countWhiteSpaces, SentenceInvestigator.switchFirstAndLastWord, and SentenceInvestigator.removeVowels are used in the code but are not provided in the translated content. You would need to define these variables and functions separately in your code.

英文:

So I wanna write a program to fulfill these requirements but have problems with using loops, cause the program simply doesn't run.
The program should displays a menu for the user to select the following action. This is also displayed again after the action is completed until the user exits the program.

  • The menu contains the following items, each of which is to be executed when the named letter is entered:
A) Enter block 
B) Determine number of spaces 
C) Swap 1st and last word 
D) Remove all vowels 
X) End
  • If the user enters a different letter, nothing happens or the menu is displayed again.

  • If menu item A is selected, a prompt is displayed to enter a block which is read into a string variable. This variable must not be changed by the actions of menu items B, C and D! If necessary a copy of the record must be made before in a further string variable.

  • If the user executes menu option B, the read in record is run through character by character and the number of blanks in it is counted and then output (e.g. "3 blanks found in record").

  • Menu item C determines the first and the last word in the sentence (i.e. everything up to the first or everything from the last blank) and then outputs a sentence in which these two parts have been interchanged.

    • Example: This turns "Nights are colder than outside" into "Outside is colder than nights".
  • To determine the position of the 1st space, please use the function indexOf. The position of the last space can then best be determined using lastIndexOf.

  • Menu item C may only work if the sentence contains at least 2 words and there is no space at the first and last position of the string. Otherwise, the error message 'The sentence must contain at least 2 words and no space must occur at the first and last positions' should appear.

  • With menu item D, all vowels (whether upper or lower case) are removed from the sentence. The new sentence is then also output.

    • Example: From "Hello grandpa, I called Emil" you get "Hll p, ch hb ml ngrfn".
  • Use a for-loop to run through all vowels, which are stored in a constant beforehand! This way you can search for one vowel at a time and delete it if necessary until all occurrences have disappeared from the string.

  • The menu items B, C and D may only be selected if a sentence has already been entered before. Otherwise, if B, C or D is entered in the menu, the error message 'No sentence has been entered yet!' is displayed and the menu is displayed again.

  • Each call of menu items B, C and D always works on the original block entered by the user and not on a block that may have already been changed by previously executed menu items! If, for example, menu item C is executed several times in succession, the result is always the same and the first and last word are not swapped again each time

  • By entering menu item A again, the entered block can also be overwritten by a new one

  • With an 'X' the user can terminate the program Translated with www.DeepL.com/Translator (free version)
    public static void main(String[] args) {

      String aktion = null;
    Scanner scanner = new Scanner(System.in);
    aktion = scanner.nextLine();
    while (!"X".equals(aktion)){
    System.out.format("A) Satz eingeben" + "%n" + "B) Anzahl Leerzeichen bestimmen" + "%n" + "C) 1. und letztes Wort vertauschen" + "%n" + "D) Alle Vokale entfernen" + "%n" + "X) Ende" + "%n");
    switch (aktion) {
    case "A":
    System.out.println("Bitte Satz eingeben:");
    str1 = scanner.nextLine();
    break;
    case "B":
    if (str1 == null) {
    System.out.println("Noch kein Satz eingegeben!");
    } else {
    System.out.println(SentenceInvestigator.countWhiteSpaces(str1) + " Leerzeichen im Satz gefunden.");
    }
    break;
    case "C":
    if (str1 == null) {
    System.out.println("Noch kein Satz eingegeben!");
    } else {
    String str2 = SentenceInvestigator.switchFirstAndLastWord(str1);
    System.out.println(str2);
    }
    break;
    case "D":
    if (str1 == null) {
    System.out.println("Noch kein Satz eingegeben!");
    } else {
    System.out.println(SentenceInvestigator.removeVowels(str1));
    }
    break;
    case "X":
    break;
    }
    }
    

    }

答案1

得分: -1

你需要学习如何使用调试器。然后你会发现变量 aktion 的值从未改变。

你正确地在每次 while 循环迭代中显示菜单,但是你只在最初进入循环之前要求用户输入他的选择一次。

将以下代码与你的代码进行比较,我还建议你使用调试器逐步进行调试。

String str1 = null;
String aktion = null;
Scanner scanner = new Scanner(System.in);
while (!"X".equals(aktion)) {
    System.out.format("A) Enter sentence" + "%n" + "B) Determine number of spaces" + "%n"
            + "C) Swap first and last word" + "%n" + "D) Remove all vowels"
            + "%n" + "X) Exit" + "%n");
    aktion = scanner.nextLine();
    switch (aktion) {
        case "A":
            System.out.println("Please enter a sentence:");
            str1 = scanner.nextLine();
            break;
        case "B":
            if (str1 == null) {
                System.out.println("No sentence entered yet!");
            }
            else {
                System.out.println(SentenceInvestigator.countWhiteSpaces(str1)
                        + " spaces found in the sentence.");
            }
            break;
        case "C":
            if (str1 == null) {
                System.out.println("No sentence entered yet!");
            }
            else {
                String str2 = SentenceInvestigator.switchFirstAndLastWord(str1);
                System.out.println(str2);
            }
            break;
        case "D":
            if (str1 == null) {
                System.out.println("No sentence entered yet!");
            }
            else {
                System.out.println(SentenceInvestigator.removeVowels(str1));
            }
            break;
        case "X":
            break;
    }
}
英文:

You need to learn how to use a debugger. Then you would discover that the value of variable aktion never changes.

You correctly display the menu for each iteration of the while loop but you only ever ask the user to enter his selection once – and that is before initially entering the loop.

Compare the following code with yours and I also recommend stepping through it with a debugger.

String str1 = null;
String aktion = null;
Scanner scanner = new Scanner(System.in);
while (!"X".equals(aktion)) {
    System.out.format("A) Satz eingeben" + "%n" + "B) Anzahl Leerzeichen bestimmen" + "%n"
            + "C) 1. und letztes Wort vertauschen" + "%n" + "D) Alle Vokale entfernen"
            + "%n" + "X) Ende" + "%n");
    aktion = scanner.nextLine();
    switch (aktion) {
        case "A":
            System.out.println("Bitte Satz eingeben:");
            str1 = scanner.nextLine();
            break;
        case "B":
            if (str1 == null) {
                System.out.println("Noch kein Satz eingegeben!");
            }
            else {
                System.out.println(SentenceInvestigator.countWhiteSpaces(str1)
                        + " Leerzeichen im Satz gefunden.");
            }
            break;
        case "C":
            if (str1 == null) {
                System.out.println("Noch kein Satz eingegeben!");
            }
            else {
                String str2 = SentenceInvestigator.switchFirstAndLastWord(str1);
                System.out.println(str2);
            }
            break;
        case "D":
            if (str1 == null) {
                System.out.println("Noch kein Satz eingegeben!");
            }
            else {
                System.out.println(SentenceInvestigator.removeVowels(str1));
            }
            break;
        case "X":
            break;
    }
}

huangapple
  • 本文由 发表于 2020年10月27日 19:14:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64553232.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定