Java 如何用破折号或问号替换字符串?
所以我正在制作一个刽子手游戏,在这个游戏中,用户输入一个单词,然后这个单词会显示出来,但会伪装成问号。每当用户正确输入一个单词时,字母就会出现在问号的位置。我有猜测等所需的所有代码,我只需要有人帮助掩饰用户在问号中输入的单词Java 如何用破折号或问号替换字符串?,java,string,Java,String,所以我正在制作一个刽子手游戏,在这个游戏中,用户输入一个单词,然后这个单词会显示出来,但会伪装成问号。每当用户正确输入一个单词时,字母就会出现在问号的位置。我有猜测等所需的所有代码,我只需要有人帮助掩饰用户在问号中输入的单词 public static void main(String[] args) { Scanner Bob = new Scanner(System.in); ArrayList<String> alphabet = new ArrayList&l
public static void main(String[] args) {
Scanner Bob = new Scanner(System.in);
ArrayList<String> alphabet = new ArrayList<String>(Arrays.asList("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"));//i looked this part up Mr. A, I didn't know how to directly set values in an arraylist
int guesses = 6;//wrong guesses
System.out.println("Hello! Please enter in your word for the game. Only one word is allowed!");
String word = Bob.next();
int lettersInWord = word.length();//so you know if you've solved the word or not
while(guesses > 0 && lettersInWord > 0){ //not or because you'll end up getting a negative 1
System.out.println("What is your guess? One letter at a time please or you'll mess up my game.");
String guess = Bob.next();
guess.toLowerCase(); //i populated the arraylist with only lowercase letters so it has to be this way
while(!alphabet.contains(guess)){ //since I remove letters from the arraylist, this is like the default to make sure a guess doesn't happen twice
System.out.println("You already guessed this letter, or this is not a letter");
guess = Bob.next();//just lets user make another guess
}
alphabet.remove(guess);//takes letter out of alphabet so no redoes happen
if(word.contains(guess)){
System.out.println("Yeauhhhhh, thats right! You guessed " + guess);
lettersInWord--;//makes the word size smaller
} else {
System.out.println("Nahhhhh, that's wrong! You guessed " + guess + " and it is not in the word");
guesses--;//takes a guess away
}
System.out.println("You have " + guesses + " wrong guesses left"); //displays at the end of the iteration
}
if(guesses == 0){//this comes up when you lose
System.out.println("You have lost, the word was "+ word);
} else {//this comes up when you've guessed the word, if its not one its the other!!!
System.out.println("You have won! the word was "+ word + "good job dude. Please see\n"
+ "Mr. A for a free piece of cake and a 100 on your final.");
}
}
publicstaticvoidmain(字符串[]args){
扫描器Bob=新扫描器(System.in);
ArrayList alphabet=新的ArrayList(Arrays.asList(“a”、“b”、“c”、“d”、“e”、“f”、“g”、“h”、“i”、“j”、“k”、“l”、“m”、“n”、“o”、“p”、“q”、“r”、“s”、“t”、“u”、“v”、“w”、“x”、“y”、“z”);//我查了这部分,a先生,我不知道如何直接在ArrayList中设置值
int猜测=6;//错误猜测
System.out.println(“您好!请为游戏输入您的单词。只允许输入一个单词!”);
String word=Bob.next();
int lettersInWord=word.length();//这样你就知道你是否解出了这个词
而(猜测>0&&lettersInWord>0){//not或者因为您将得到一个负1
System.out.println(“你猜怎么着?请一次一封信,否则你会把我的游戏搞砸的。”);
字符串guess=Bob.next();
guess.toLowerCase();//我在arraylist中只填充了小写字母,所以必须这样
虽然(!alphabet.contains(guess)){//因为我从arraylist中删除了字母,这类似于默认设置,以确保猜测不会发生两次
System.out.println(“您已经猜到了这封信,或者这不是一封信”);
guess=Bob.next();//让用户再猜一次
}
字母表。删除(猜测);//从字母表中删除字母,这样就不会重做
if(单词包含(猜测)){
System.out.println(“是啊,没错!你猜对了”+猜对了);
lettersInWord--;//使字号变小
}否则{
System.out.println(“不,那是错的!你猜了“+guess+”,它不在单词中”);
猜测--;//消除猜测
}
System.out.println(“您还有“+猜测+”错误猜测”);//在迭代结束时显示
}
如果(猜测==0){//当你输的时候会出现这个问题
System.out.println(“你输了,单词是“+单词”);
}否则{//当你猜到这个词时,这个词就会出现,如果不是一个,那就是另一个!!!
System.out.println(“你赢了!单词是“+word+”干得好,伙计。请看\n”
+“A先生免费一块蛋糕,期末考试100分。”);
}
}
您可以创建一个单独的问号字符串,其长度与您的单词长度相同
String marks = "";
for (int i = 0; i < word.length(); i++) {
marks += "?";
}
字符串标记=”;
for(int i=0;i
当一个字母猜对了,你就用这个字母替换一个问号。最后我给你举了一个简单的例子
public class Test {
public static void main(String args[]) {
String word = "Hello";
String unCapWord = word.toLowerCase();
int guesses = word.length();
int count = 0;
char usedLetters[] = new char[26];
String testWin;
char[] guessBox = new char[word.length()];
for (int i =0; i < word.length(); i++)
guessBox[i] = '?';
Scanner bob = new Scanner(System.in);
while (count < guesses) {
char guess = bob.next().toLowerCase().charAt(0);
String checkIfCharUsed = new String(usedLetters);
if (checkIfCharUsed.contains(guess+"")) {
System.out.println("Already used this char, try another!");
continue;
}
for (int i =0; i < word.length(); i++) {
if (unCapWord.charAt(i) == guess)
guessBox[i] = guess;
}
testWin = new String(guessBox);
if (testWin.equals(unCapWord)) {
System.out.println("You got it!");
return;
}
usedLetters[count] = guess;
count++;
}
System.out.println("You lost!");
}
}
公共类测试{
公共静态void main(字符串参数[]){
String word=“Hello”;
字符串unCapWord=word.toLowerCase();
int guesses=word.length();
整数计数=0;
char usedLetters[]=新字符[26];
字符串testWin;
char[]猜测框=新字符[word.length()];
for(int i=0;i
这是一个快速而肮脏的操作,但会为被猜测的单词中的每个字母显示破折号。当您猜测正确的字母时,破折号将替换为猜测的字母
public static void main(String[] args) throws Exception {
Scanner Bob = new Scanner(System.in);
ArrayList<String> alphabet = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"));//i looked this part up Mr. A, I didn't know how to directly set values in an arraylist
int guesses = 6;//wrong guesses
System.out.println("Hello! Please enter in your word for the game. Only one word is allowed!");
String word = Bob.next();
int lettersInWord = word.length();//so you know if you've solved the word or not
String[] lettersOfWord = new String[lettersInWord];
// Replace "-" with "?" if you want to use question marks
Arrays.fill(lettersOfWord, "-");
clearScreen();
String guess = "";
int lettersCorrect = -1;
while (guesses > 0 && lettersInWord > 0) { //not or because you'll end up getting a negative 1
if (lettersCorrect > 0) {
System.out.println(String.format("There %s %d %s",
lettersCorrect > 1 ? "are" : "is", lettersCorrect, guess));
lettersCorrect = 0;
} else if (lettersCorrect == 0) {
System.out.println("Nahhhhh, that's wrong! You guessed " + guess + " and it is not in the word");
}
System.out.println(String.join("", lettersOfWord));
System.out.println("");
System.out.println("You have " + guesses + " wrong guesses left"); //displays at the end of the iteration
System.out.println("What is your guess? One letter at a time please or you'll mess up my game.");
guess = Bob.next().toLowerCase();
while (!alphabet.contains(guess)) { //since I remove letters from the arraylist, this is like the default to make sure a guess doesn't happen twice
System.out.println("You already guessed this letter, or this is not a letter");
guess = Bob.next();//just lets user make another guess
}
alphabet.remove(guess);//takes letter out of alphabet so no redoes happen
if (word.contains(guess)) {
for (int i = 0; i < word.length(); i++) {
if (guess.equals(String.valueOf(word.charAt(i)))) {
lettersOfWord[i] = guess;
lettersInWord--;
lettersCorrect++;
}
}
} else {
guesses--;//takes a guess away
}
clearScreen();
}
if (guesses == 0) {//this comes up when you lose
System.out.println("You have lost, the word was " + word);
} else {//this comes up when you've guessed the word, if its not one its the other!!!
System.out.println("You have won! the word was " + word + " good job dude. Please see\n"
+ "Mr. A for a free piece of cake and a 100 on your final.");
}
}
// Simulate a clear screen
private static void clearScreen() {
for(int clear = 0; clear < 1000; clear++) {
System.out.println("\b") ;
}
}
publicstaticvoidmain(字符串[]args)引发异常{
扫描器Bob=新扫描器(System.in);
ArrayList alphabet=新的ArrayList(Arrays.asList(“a”、“b”、“c”、“d”、“e”、“f”、“g”、“h”、“i”、“j”、“k”、“l”、“m”、“n”、“o”、“p”、“q”、“r”、“s”、“t”、“u”、“v”、“w”、“x”、“y”、“z”);//我查了这部分,a先生,我不知道如何直接在ArrayList中设置值
int猜测=6;//错误猜测
System.out.println(“您好!请为游戏输入您的单词。只允许输入一个单词!”);
String word=Bob.next();
int lettersInWord=word.length();//这样你就知道你是否解出了这个词
String[]lettersOfWord=新字符串[lettersOfWord];
//如果要使用问号,请将“-”替换为“?”
数组。填充(字母sofword,“-”;
清除屏幕();
字符串猜测=”;
int-letterscorect=-1;
而(猜测>0&&lettersInWord>0){//not或者因为您将得到一个负1
如果(LettersRect>0){
System.out.println(String.format(“There%s%d%s”),
LettersRect>1?“是”:“是”,LettersRect,猜测);
LettersRect=0;
}否则如果(LettersRect==0){
System.out.println(“不,那是错的!你猜了“+guess+”,它不在单词中”);
}
System.out.println(String.join(“,lettersOfWord));
System.out.println(“”);
System.out.println(“您还有“+猜测+”错误猜测”);//在迭代结束时显示
你猜怎么着?一个字母a