gpt4 book ai didi

Java:如果用户输入不是单个字母小写字母,我如何将用户返回到主菜单?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:18:45 26 4
gpt4 key购买 nike

我正在尝试运行一个允许用户输入 char keyCharacter 和 String theString 的程序。然后,使用这些输入,如果 keyCharacter 在 theString 中出现,我将用“$”屏蔽它,从 theString 中删除 keyCharacter,最后,计算 keyCharacter 在 theString 中出现的总次数。

除了用户必须输入字符的 getKeyCharacter 方法外,每个方法都工作正常:用户只能输入一个字母(例如 q 或 z)。

如果用户输入的不是单个字母(可以是单词、短语、句子、# 或 $ 等特殊字符、空格或制表符,或者只是按回车)以外的任何内容,则程序会返回用户到要求用户提供 keyCharacter 的原始问题。这应该继续循环回到原来的问题,直到用户输入有效的输入。

由于我还是java的初学者,到目前为止循环是我的弱点,这部分给我带来了很多麻烦。我知道我应该使用 while 循环,嵌套循环背后的逻辑让我很困惑。

通过搜索可能的解决方案,我知道这些称为正则表达式和 try-catch 异常的东西可以帮助解决我的问题,但由于我们没有在类里面明确地讨论过这些,我不想处理现在。谢谢。

这是我的代码粘贴:

import java.util.*;

public class Foothill {

// main method
public static void main (String[] args) {
char keyCharacter = getKeyCharacter();
String theString = getString();
maskCharacter(theString, keyCharacter);
countKey(theString, keyCharacter);
removeCharacter(theString, keyCharacter);
}

// get keyCharacter
public static char getKeyCharacter() {
Scanner inputStream = new Scanner(System.in);
boolean stop = false;
String firstPrompt, strKeyCharacter;
char keyCharacter = ' ';

while (stop != true) {
firstPrompt = "Please enter a SINGLE character to act as key: ";
System.out.print(firstPrompt);
strKeyCharacter = inputStream.nextLine();

while (strKeyCharacter.length() != 1) {
firstPrompt = "Please enter a SINGLE character to act as key: ";
System.out.print(firstPrompt);
strKeyCharacter = inputStream.nextLine();
}

keyCharacter = strKeyCharacter.charAt(0);

while (strKeyCharacter.length() == 1) {
firstPrompt = "Please enter a SINGLE character to act as key: ";
System.out.print(firstPrompt);
strKeyCharacter = inputStream.nextLine();
if (keyCharacter == 'a' || keyCharacter == 'b' || keyCharacter == 'c' || keyCharacter == 'd'
|| keyCharacter == 'e' || keyCharacter == 'f' || keyCharacter == 'g' || keyCharacter == 'h'
|| keyCharacter == 'i' || keyCharacter == 'j' || keyCharacter == 'k' || keyCharacter == 'l'
|| keyCharacter == 'm' || keyCharacter == 'n' || keyCharacter == 'o' || keyCharacter == 'p'
|| keyCharacter == 'q' || keyCharacter == 'r' || keyCharacter == 's' || keyCharacter == 't'
|| keyCharacter == 'u' || keyCharacter == 'v' || keyCharacter == 'w' || keyCharacter == 'x'
|| keyCharacter == 'y' || keyCharacter == 'z') {
System.out.println("You entered: " + keyCharacter + '\n');
stop = true;
} else {
break;
}
}
}
return keyCharacter;
}

// declare final = 4 to be constant
public static final int minimumLength = 4;

// get theString
public static String getString() {
Scanner inputStream = new Scanner(System.in);
String secondPrompt, theString;
do {
secondPrompt = "Please enter a phrase or sentence >= 4: ";
System.out.print(secondPrompt);
theString = inputStream.nextLine();
System.out.print('\n');
} while (theString.length() < minimumLength || theString == null || theString.length() == 0);
inputStream.close();
return theString;
}

// mask keyCharacter with $
public static String maskCharacter(String theString, char keyCharacter) {
theString = theString.replace(keyCharacter, '$');
System.out.println("String with " + " '" + keyCharacter + "' " + " masked.");
System.out.println(theString + '\n');
return theString;
}

// count number of times keyCharacter occurs in theString
public static void countKey(String theString, char keyCharacter) {
int countChar = 0;
for (int charTimes = 0; charTimes < theString.length(); charTimes++) {
if (theString.charAt(charTimes) == keyCharacter) {
countChar++;
}
}
System.out.println( "The key character occurs " + countChar + " times. \n");
return;
}

// remove keyCharacter from theString
public static void removeCharacter(String theString, char keyCharacter) {
theString = theString.replace(String.valueOf(keyCharacter), "");
System.out.println("String with " + "'" + keyCharacter + "' removed: ");
System.out.println(theString);
return;
}
}

这是我运行的粘贴(如您所见,我的程序中需要进行一些认真的调试):

Please enter a SINGLE character to act as key: f
Please enter a SINGLE character to act as key: f
You entered: f

Please enter a SINGLE character to act as key: f
You entered: f

Please enter a SINGLE character to act as key: f
You entered: f

Please enter a SINGLE character to act as key: f
You entered: f

Please enter a SINGLE character to act as key:

// which then continues so on so forth...

最佳答案

public static char getKeyCharacter(){

Scanner inputStream = new Scanner(System.in);
boolean stop = false;
String firstPrompt, strKeyCharacter;
char keyCharacter = ' ';

while(!stop){

firstPrompt = "Please enter a SINGLE character to act as key: ";
System.out.println(firstPrompt);
strKeyCharacter = inputStream.nextLine();

//check if the input contains only 1 character
boolean isSingleChar = (strKeyCharacter.length() == 1);
//check if the input character is within the ASCII code of 97 (a) to 122 (z)
boolean isValidChar =
strKeyCharacter.charAt(0) >= 97 &&
strKeyCharacter.charAt(0) <= 122;

if(isSingleChar && isValidChar){
keyCharacter = strKeyCharacter.charAt(0);
stop = true;
}


}

return keyCharacter;
}

关于Java:如果用户输入不是单个字母小写字母,我如何将用户返回到主菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31467970/

26 4 0