我被困在一个类的java任务中,我们需要制作一个Hangman游戏但是一个真正的基础游戏(它是Java类的介绍).基本上我有一个人输入的单词而另一个人必须猜测这个单词,但是他们没有看到这个单词所以它显示它就像这样(例如,如果单词是aardvark)
********
然后用户输入一个字母,如果它的部分单词然后显示那些字母,例如:
输入字母:a
aa***a**
输入字母:k
aa***a*k
输入字母:r
aar**ark
所以...所以是的,我已经坚持了一段时间,我真的需要帮助谢谢
PS:这是一个介绍类,所以到目前为止我所知道的都是循环(for,while,do while等),if,if/else,switch语句等.
import java.util.Scanner;
public class ass_2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// public static final Comparator<secretWord> CASE_INSENSITIVE_ORDER;
int attempts = 10;
int wordLength;
boolean solved;
Scanner userInput = new Scanner(System.in);
System.out.println("OK Guessing Player ... turn around, while your friend enters the word to guess!\n");
System.out.println("Other Player ? Enter your word (letters only, no repeated letters and not case sensitive):");
String secretWord = userInput.next();
// 20 blank spaces WITH a for loop, we're smart!
for(int i = 1; i <= 20; i++)
System.out.print("\n");
Scanner userLetter = new Scanner(System.in);
String letter;
System.out.print("Word to date: ");
for (int i = 0; i < secretWord.length(); i++)
{
System.out.print("*");
}
while (attempts <= 10 && attempts > 0)
{
System.out.println("\nAttempts left: " + attempts);
System.out.print("Enter letter: ");
attempts--;
}
System.out.println("\n---------------------------");
System.out.println("Sorry you didn't find the mystery word!");
System.out.println("It was \"" + secretWord + "\"");
}
Run Code Online (Sandbox Code Playgroud)
}
嗨乔丹,您的第一次尝试看起来非常好!在while循环中,您只需要更多逻辑即可读取猜测并用正确的猜测替换“ *”。而且我建议您将混淆后的单词(“ ***** ...”)存储在字符串中,而不是仅将其打印出来,稍后再用。
根据您的代码判断,您不需要用户输入任何帮助,唯一的问题是用正确的猜测正确替换星号,让我们开始吧:
String secret;
//read in secret string
String displaySecret;
//generate as many "*"s as secret is long and store them in displaySecret
Run Code Online (Sandbox Code Playgroud)
现在很酷的事情是这样的:
...没有重复的信件...
这将使您的作业更加轻松!查看Williwaw提供的String类的文档。在那里,您将找到两种方法可以得出解决方案:
我认为您可以轻松找到解决方案。随时在评论中提出其他问题!
编辑:更多帮助
String secret = "example-text";
String displaySecret = "";
for (int i = 0; i < secret.length(); i++)
displaySecret += "*";
char guess;
//read in a guess
int position = secret.indexOf(guess);
//now position contains the index of guess inside secret, or
//-1 if the guess was wrong
String newDisplaySecret = "";
for (int i = 0; i < secret.length(); i++)
if (i == position)
newDisplaySecret += secret.charAt(i); //newly guessed character
else
newDisplaySecret += displaySecret.charAt(i); //old state
displaySecret = new String(newDisplaySecret);
Run Code Online (Sandbox Code Playgroud)
该死的,我确定有某种setCharAt(int)方法..循环完成了工作。
存储与秘密单词长度相同的字符数组。将字符初始化为*,当找到匹配项时,使用[indexOf][1],显示找到的字符:
String secretWord = userInput.next();
int len = secretWord.length(); //Store the length which will be used to see if puzzle was solved.
char[] temp = new char[len]; //Store a temp array which will be displayed to the user
for(int i = 0; i < temp.length; i++) //initialize the array
{
temp[i] = '*';
}
System.out.print("\n");
System.out.print("Word to date: ");
while (attempts <= 10 && attempts > 0)
{
System.out.println("\nAttempts left: " + attempts);
System.out.print("Enter letter: ");
String test = userInput.next();
if(test.length() != 1)
{
System.out.println("Please enter 1 character");
continue;
}
char testChar = test.charAt(0);
//Find matches
int foundPos = -2;
int foundCount = 0; //How many matches did we find
while((foundPos = secretWord.indexOf(testChar, foundPos + 1)) != -1)
{
temp[foundPos] = testChar; //Update the temp array from * to the correct character
foundCount++;
len--; //Decrease overall counter
}
if(foundCount == 0)
{
System.out.println("Sorry, didn't find any matches for " + test);
}
else
{
System.out.println("Found " + foundCount + " matches for " + test);
}
//Print
for(int i = 0; i < temp.length; i++)
{
System.out.print(temp[i]);
}
System.out.println();
if(len == 0)
{
break; //Solved!
}
attempts--;
}
if(len == 0)
{
System.out.println("\n---------------------------");
System.out.println("Solved!");
}
else
{
System.out.println("\n---------------------------");
System.out.println("Sorry you didn't find the mystery word!");
System.out.println("It was \"" + secretWord + "\"");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
42833 次 |
| 最近记录: |