Java 逐字猜字符串
我有一个任务,我正在努力,我想知道是否有人可以帮助我。 我应该创建一个程序来猜测字符串中的正确字母。我有一个名为alphabet的字符串,其中包含字母表中的所有字母,还有一个由用户输入的字符串。当用户猜到正确的字母时,字母会显示出来,未使用的字母在字符串中保留为问号,如下所示。比如说字符串是I like popcorn,用户猜p应该是这样的:????p?p????。然后说他们猜到另一个像我一样的正确字母,它会变成:i?i??p?p????。这是我的密码,我不知道该怎么办Java 逐字猜字符串,java,string,words,Java,String,Words,我有一个任务,我正在努力,我想知道是否有人可以帮助我。 我应该创建一个程序来猜测字符串中的正确字母。我有一个名为alphabet的字符串,其中包含字母表中的所有字母,还有一个由用户输入的字符串。当用户猜到正确的字母时,字母会显示出来,未使用的字母在字符串中保留为问号,如下所示。比如说字符串是I like popcorn,用户猜p应该是这样的:????p?p????。然后说他们猜到另一个像我一样的正确字母,它会变成:i?i??p?p????。这是我的密码,我不知道该怎么办 import java.
import java.util.Scanner;
public class Characters {
// This class controls a while loop
// use at least 7 methods
public static void charactersMethod() throws InterruptedException
{
Scanner scan = new Scanner(System.in);
String choice;
Introduction();
readString();
guessChar();
do {
Scanner Scan = new Scanner (System.in);
System.out.println("Would you like to play this game again? (Yes/No)");
choice = Scan.next();
}
while (choice.equalsIgnoreCase("Yes"));
}
private static void Introduction() throws InterruptedException {
System.out.println("Welcome to characters game!");
System.out.println("This game prompts for a string of words. it then allows the player to guess the characters in the string.");
System.out.println("it then keeps track of all of the characters guessed and tells the player he wins when");
System.out.println("he guesses all the correct characters in the string.");
}
public static void readString() throws InterruptedException {
Scanner Keyboard = new Scanner(System.in);
String originalString;
String guessedString;
System.out.println("Please enter a string");
originalString = Keyboard.nextLine();
System.out.println("original string: " + originalString);
System.out.println("guessed string: ?????????");
System.out.println();
System.out.println("Characters to choose from: abcdefghijklmnopqrstuvwxyz.");
// when a correct letter is guessed, originalString - guess;
}
public static void guessChar() throws InterruptedException
{
Scanner Scan = new Scanner (System.in);
String guess;
System.out.println("Please guess a character: ");
guess = Scan.nextLine();
System.out.println("Character read: " + guess);
while (guess.equalsIgnoreCase("A")) {
System.out.println("The new String is " + originalString);
}
}
public static void getRidChar() throws InterruptedException {
}
private static void sleepDemonstration() throws InterruptedException
{
}
}
您可以创建两个字符数组,一个是要猜测的字符串,另一个是字符加?这已经被猜测到了
//Create char array out of String
char[] originalCharArray = originalString.toCharArray();
//Pass the two char arrays plus the guess to checkGuess
public char[] checkGuess (char[] originalCharArray, char[] guessedCharArray, Char guess) {
for (int i = 0; i < originalCharArray.length; i++) {
if (orignalCharArray[i] == guess) {
guessedStringArray[i] = guess;
}
}
}
//This method initializes the guess array to '?'
public char[] init(char[] guessedStringArray) {
for(int i = 0; i < guessedStringArray.length; i++) {
guessedStringArray[i] = '?';
}
}
我多么怀念我能写真正有趣的节目的时光啊-您的guessChar功能有点不正常。你读的是一行,而不是一个字符,然后说,当这个字符串等于时,不管大小写是什么,打印出这个字符串。那不是你真正想做的。我多么希望我有20分钟的时间坐下来为你写这封信:而不是做whileguess.equalsIgnoreCaseA do guessString=originalString;那么forint i=0;i