gpt4 book ai didi

java - return 导致程序要求输入两次

转载 作者:太空宇宙 更新时间:2023-11-04 13:50:57 25 4
gpt4 key购买 nike

我编写了以下程序,它执行了预期的操作,除了它要求输入两次之外。我确信它要求输入两次的原因是因为 返回,因为当我在 void 方法中尝试相同的代码时,它只要求输入一次。

import java.util.Scanner;

public class Foothill
{
public static void main(String[] args)
{
String theString, maskString, removeString;
char keyCharacter;
int keyCount;
input = new Scanner(System.in);

getKeyCharacter();
keyCharacter = getKeyCharacter();
getString();
theString = getString();
maskCharacter(theString , keyCharacter);
maskString = maskCharacter(theString , keyCharacter);
removeCharacter(theString , keyCharacter);
removeString = removeCharacter(theString , keyCharacter);
countKey(theString , keyCharacter);
keyCount = countKey(theString , keyCharacter);
input.close();

System.out.println("String with '" + keyCharacter + "' masked");
System.out.println("" + maskString + SEPERATOR);
System.out.println("String with '" + keyCharacter + "' removed");
System.out.println("" + removeString + SEPERATOR);
System.out.println("# of " + keyCharacter + ": " + keyCount);
}

public static char getKeyCharacter()
{
String userInput;
char keyCharacter;
int characterLength;

do
{
System.out.println("Please enter a SINGLE letter to act as key: ");

userInput = input.next();
characterLength = userInput.length();

}
while (characterLength != 1);

keyCharacter = userInput.charAt(0);
return keyCharacter;
}

public static String getString()
{
String theString;
int stringLength;

do
{
System.out.println("Please enter a phrase or sentence >= 4 "
+ "characters: ");

theString = input.next();
stringLength = theString.length();

}
while (stringLength < MIN_CHAR || stringLength > MAX_CHAR);

return theString;
}
}

最佳答案

替换:

getKeyCharacter();
keyCharacter = getKeyCharacter();
getString();
theString = getString();

与:

keyCharacter = getKeyCharacter();
theString = getString();

关于java - return 导致程序要求输入两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30283065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com