Java 制作刽子手游戏
逻辑: 这就是输出的样子。我需要在原始字符串中找到猜测的索引。如果这是真的,那么它应该用字符串猜测中读取的字符替换索引中的问号。之后,它应该从字符串“abcdefghijklmnopqrstuvxyz”中删除该字符 如果originalString不包含guess,那么它应该只从字符串“abcdefghijklmnopqrstuvwxyz”中删除该字符。我在谷歌上查找了这个问题,发现了一堆代码,它们都使用数组或我在课堂上没有学到的东西。所以请不要使用数组。我被if-else语句卡住了。有没有办法不使用数组就解决这个问题Java 制作刽子手游戏,java,arrays,string,Java,Arrays,String,逻辑: 这就是输出的样子。我需要在原始字符串中找到猜测的索引。如果这是真的,那么它应该用字符串猜测中读取的字符替换索引中的问号。之后,它应该从字符串“abcdefghijklmnopqrstuvxyz”中删除该字符 如果originalString不包含guess,那么它应该只从字符串“abcdefghijklmnopqrstuvwxyz”中删除该字符。我在谷歌上查找了这个问题,发现了一堆代码,它们都使用数组或我在课堂上没有学到的东西。所以请不要使用数组。我被if-else语句卡住了。有没有办法
int count=1;
while (count<=24){
Scanner keyboard = new Scanner(System.in);
int length;
String originalString;
String guess;
String option= "abcdefghijklmnopqrstuvwxyz";
String questionmarks;
System.out.println("Please enter a string");
originalString=keyboard.nextLine();
length=originalString.length();
questionmarks = originalString.replaceAll(".", "?");
System.out.println("Original String: "+originalString);
System.out.println("Guessed String: "+questionmarks);
System.out.println("Characters to choose from: "+option);
System.out.println("Please guess a character");
guess=keyboard.nextLine();
if (originalString.contains(guess)){
count++;
}
else{
option.replace(guess, "_");
count++;
System.out.println(option);
}
int count=1;
而(数一数我从粗略的一瞥中注意到的几件事:
.replace()
返回一个字符串
,它不会修改选项
,除非您执行以下操作:
option=option.replace(猜测“”);
- 另外,由于您不想使用数组,我强烈建议您使用
编辑1(基于重复线程的评论):
您可以使用StringBuilder
创建一个初始化为所有-
的字符串。然后当有人猜到正确的字母时,您可以用猜出的-
替换-
StringBuilder sb_word = new StringBuilder(lengthOfOriginalString);
for (int i = 0; i < length; i++)
sb_word.append('-'); //add hyphens to StringBuilder, StringBuffer would also work
然后,在有人做出guess
之后,如果您已确定位置i
处的字符应替换为guess
,您可以执行以下操作:
sb_word.setCharAt(i, guess.charAt(0));
编辑2:
while (bodyparts > 0 && !win) //play game while you have bodyparts and haven't won
{
System.out.printf("Word to guess: %s\nEnter a letter or word guess: " , sb_word);
guess = keyboard.next();
if (guess.length() == 1)
{
for (int i = 0; i < length; i++) //loop to see if guess is in originalString
if (Character.toLowerCase(word.charAt(i)) ==
Character.toLowerCase(guess.charAt(0)))
{ //it is, so set boolean contains to be true and replace blank with guess
sb_word.setCharAt(i, guess.charAt(0));
contains = true;
}
if (!contains)
{
bodyparts--;
System.out.printf("Incorrect, you have %d bodyparts left.\n", bodyparts);
}
else if (sb_word.indexOf(String.valueOf(blank)) == -1)
{ //all the letters have been uncovered, you win
win = true;
System.out.println(word);
}
else
{
contains = false;
System.out.println("Good guess.");
}
}
else
{
if (guess.equals(word))
win = true;
else
{
bodyparts = 0;
System.out.printf("Incorrect, you have %d bodyparts left.\n" , bodyparts);
}
}
}
while(bodyparts>0&&!win)//在你有bodyparts但没有赢的时候玩游戏
{
System.out.printf(“单词猜测:%s\n输入字母或单词猜测:”,sb\u单词);
猜测=键盘。下一步();
if(guess.length()==1)
{
for(int i=0;i
自从你第一次提问以来,你没有取得任何进展吗?你得到的答案对你没有帮助吗?为什么?@assylias有办法将问题标记为重复问题吗?@deactivator 2因为你还没有足够的声誉,你不能投票关闭,但你可以标记它。@SteveP.完成,谢谢。在我发布此消息之前,没有人回答,而不是在我的o中回答deactivator当我发布thispoption=option.replace(猜,“"”)时,我的问题是正确的;我们也没有学过stringbuilder。因此,如果猜错了字符,我的代码就可以工作。如果他们猜对了字符,有什么建议吗?@Treasure我很少这么做,但你似乎很迷茫。你必须采取主动,自己学习。谁在乎你没有学过stringbuilder呢,去学吧.Google,兄弟。别指望有人给你代码。不是所有的东西都是显式的,比如变量声明之类的。我会让你自己去弄清楚,但逻辑是正确的。祝你好运。@PS:如果你复制这个代码并把它作为你的作业交上来:(a)你的老师或助教或其他你最有可能知道这不是你的,(b)这只是剽窃,(c)不要做一些愚蠢的事情,比如简单地改变变量名,这并不像人们想象的那么聪明(在过去5年里一直是助教,总是抓住作弊的人)。祝你好运。
while (bodyparts > 0 && !win) //play game while you have bodyparts and haven't won
{
System.out.printf("Word to guess: %s\nEnter a letter or word guess: " , sb_word);
guess = keyboard.next();
if (guess.length() == 1)
{
for (int i = 0; i < length; i++) //loop to see if guess is in originalString
if (Character.toLowerCase(word.charAt(i)) ==
Character.toLowerCase(guess.charAt(0)))
{ //it is, so set boolean contains to be true and replace blank with guess
sb_word.setCharAt(i, guess.charAt(0));
contains = true;
}
if (!contains)
{
bodyparts--;
System.out.printf("Incorrect, you have %d bodyparts left.\n", bodyparts);
}
else if (sb_word.indexOf(String.valueOf(blank)) == -1)
{ //all the letters have been uncovered, you win
win = true;
System.out.println(word);
}
else
{
contains = false;
System.out.println("Good guess.");
}
}
else
{
if (guess.equals(word))
win = true;
else
{
bodyparts = 0;
System.out.printf("Incorrect, you have %d bodyparts left.\n" , bodyparts);
}
}
}