我正在尝试运行一个允许用户同时输入char keyCharacter和String theString的程序。然后,使用这些输入,我将屏蔽keyCharacter,如果它出现在带有“$”的字符串中,从theString中删除keyCharacter,最后计算keyCharacter在字符串中出现的次数。
除了方法getKeyCharacter之外,每个方法都正常工作,用户必须输入char: 用户只能输入一个字母(例如q或z)。
如果用户输入的不是该单个字母(可以是单词,短语,句子,特殊字符,如#或$,空格或制表符,或只是按Enter键),则程序返回用户来自用户请求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...
答案 0 :(得分:2)
//create texts
writeText(canvas, "Money: $" + player.getMoney(), Paint.Align.CENTER, Color.BLACK, 25, (screenWidth / 2), 50);
writeText(canvas, "MPS: " + player.getMPS(), Paint.Align.CENTER, Color.BLACK, 25, (screenWidth / 2), 75);
writeText(canvas, "MPC: " + player.getMPC(), Paint.Align.CENTER, Color.BLACK, 25, (screenWidth / 2), 100);
writeText(canvas, "Cost: $" + player.getHouseUpgradeCost(), Paint.Align.RIGHT, Color.BLACK, 15, 0, (screenHeight - buttonHeight));
writeText(canvas, "Cost: $" + player.getConstructionWorkerCost(), Paint.Align.CENTER, Color.BLACK, 15, (screenWidth / 2), (screenHeight - buttonHeight));
writeText(canvas, "Cost: $" + player.getBobCost(), Paint.Align.LEFT, Color.BLACK, 15, screenWidth, (screenHeight - buttonHeight));
writeText(canvas, "Cost: $" + player.getWorkGroupCost(), Paint.Align.RIGHT, Color.BLACK, 15, 0, ((screenHeight - (buttonHeight * 3))));
writeText(canvas, "Cost: $" + player.getBankCost(), Paint.Align.CENTER, Color.BLACK, 15, (screenWidth / 2), ((screenHeight - (buttonHeight * 3))));
writeText(canvas, "Cost: $" + player.getFactoryCost(), Paint.Align.LEFT, Color.BLACK, 15, (screenWidth), ((screenHeight - (buttonHeight * 3))));
writeText(canvas, "Cost: $" + player.getSuperTappingCost(), Paint.Align.RIGHT, Color.BLACK, 15, 0, ((screenHeight - (buttonHeight * 5))));
writeText(canvas, "Cost: $" + player.getUnlockStoreCost(), Paint.Align.CENTER, Color.BLACK, 15, (screenWidth / 2), ((screenHeight - (buttonHeight * 5))));
writeText(canvas, "Cost: $" + player.getPropertyCost(), Paint.Align.LEFT, Color.BLACK, 15, (screenWidth), ((screenHeight - (buttonHeight * 5))));
}
private void writeText(Canvas canvas, String text, Paint.Align align, int color, int textSize, float x, float y) {
//Sets paint using values
Paint paint = new Paint();
paint.setTextAlign(align);
paint.setColor(color);
paint.setTextSize(textSize);
canvas.drawText(text, x, y, paint);
}