Java 命运之轮:建筑之弦
我正在为一项作业编写代码,在这项作业中,我必须制作命运之轮,我在构建字符串时遇到了麻烦,在每次猜测之后,字符串都会显示给用户。我让轮子转起来了,但我似乎不知道怎么做绳子。如果你有任何疑问,我也可以回答你。任何帮助都将不胜感激。以下是我目前的代码:Java 命运之轮:建筑之弦,java,Java,我正在为一项作业编写代码,在这项作业中,我必须制作命运之轮,我在构建字符串时遇到了麻烦,在每次猜测之后,字符串都会显示给用户。我让轮子转起来了,但我似乎不知道怎么做绳子。如果你有任何疑问,我也可以回答你。任何帮助都将不胜感激。以下是我目前的代码: class WheelOfFortune3 { public static void main(String[] args) { int result, location, newLocation, numGuess = 0;
class WheelOfFortune3 {
public static void main(String[] args) {
int result, location, newLocation, numGuess = 0;
String puzzle, guess;
String sub[] = new String[26];
for (int i = 0; i < sub.length; i++) {
sub[i] = "_";
}
result = wheelResult();
System.out.println("You spun $" + result);
puzzle = getPuzzle();
System.out.println(puzzle);
do {
guess = In.getString();
location = checkGuess(puzzle, guess);
if (location == -1) {
System.out.println("Incorrect guess");
}
if (location >= 0 && location <= 25) {
System.out.println("Correct guess");
newLocation = location + 1;
sub[numGuess] = puzzle.substring(location, newLocation);
for (int i = 0; i <= 10; i++) {
System.out.print(sub[i] + " ");
}
System.out.println("");
numGuess = numGuess + 1;
System.out.println(numGuess);
System.out.println(sub.length);
}
}
while (numGuess < sub.length);
}
public static int checkGuess(String puzzle, String guess) {
String word = puzzle;
int location;
location = word.indexOf(guess);
return location;
}
public static int wheelResult() {
int result;
int[] wheelSpin = new int[1];
wheelSpin[0] = (int)(24 * Math.random());
if (wheelSpin[0] == 1 || wheelSpin[0] == 18 || wheelSpin[0] == 22) {
result = 200;
return result;
} else if (wheelSpin[0] == 2 || wheelSpin[0] == 5) {
result = 900;
return result;
} else if (wheelSpin[0] == 0 || wheelSpin[0] == 3 || wheelSpin[0] == 15) {
result = 250;
return result;
} else if (wheelSpin[0] == 4 || wheelSpin[0] == 6 || wheelSpin[0] == 12 || wheelSpin[0] == 16) {
result = 300;
return result;
} else if (wheelSpin[0] == 7) {
result = 1500;
return result;
} else if (wheelSpin[0] == 9 || wheelSpin[0] == 11) {
result = 700;
return result;
} else if (wheelSpin[0] == 10 || wheelSpin[0] == 14 || wheelSpin[0] == 21) {
result = 500;
return result;
} else if (wheelSpin[0] == 13) {
result = 5000;
return result;
} else if (wheelSpin[0] == 23 || wheelSpin[0] == 19) {
result = 600;
return result;
} else if (wheelSpin[0] == 8 || wheelSpin[0] == 20) {
result = 100;
return result;
} else if (wheelSpin[0] == 17) {
result = 17;
return result;
}
return -1;
}
public static String getPuzzle() {
String puzzle;
//a long ride
return "A long ride";
//01234567890
}
}
3级{
公共静态void main(字符串[]args){
int result,location,newLocation,numGuess=0;
字谜,猜猜;
String sub[]=新字符串[26];
对于(int i=0;i 如果(location>=0&&location这可以使用字符的列表和.contains()
方法来完成:
List<Character> guessedCharacters = new ArrayList<>();
这将创建一个字符串
,其中保存所有猜测的字母,并创建一个下划线,表示尚未正确猜测的字符。您是否有特定的问题?是的,这是如何创建一个字符串,用户每次猜测字母时都会添加该字符串。
StringBuilder toShowSB = new StringBuilder();
for(int i=0,n=puzzle.length;i<n;i++){
if(guessedCharacters.contains(puzzle.charAt(i))){
toShowSB.append(puzzel.charAt(i));
}else{
toShowSB.append("_");
}
}
String toShow = toShowSB.toString();