2013-08-01 134 views
0

逻辑: 这就是输出的样子。 http://prntscr.com/1is9ht我需要在orginalString中找到猜测的索引。如果这是真的,那么它应该用索引字符串中的字符替换索引处的问号。之后,它应该从字符串“abcdefghijklmnopqrstuvwxyz”中取出该字符。制作hang子手游戏

如果originalString不包含猜比它应该只需要指出,从字符串“ABCDEFGHIJKLMNOPQRSTUVWXYZ”字符我抬起头,这个问题对谷歌,发现了一堆代码,他们都使用数组或东西,我没有在课堂上学到。所以请不要使用数组。我被困在if语句中。有没有办法解决这个问题,而不使用数组。

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); 

    } 

请建议我一些代码,未实现阵列概念我的问题, 任何帮助将不胜感激

+1

自从你第一次询问[21 mnutes ago]之后,你没有取得任何进展吗?(http://stackoverflow.com/questions/17998138/creating-hangman-game-without-arrays)?你没有得到答案吗?为什么? – assylias

+0

@assylias是否有将问题标记为重复的方法? – Deactivator2

+0

@ Deactivator2由于您尚未拥有足够的声望,因此无法投票结束,但您可以举报。 –

回答

0

,我从一个粗略地看一眼发现有几件事情:

  • .replace()返回String,也不会修改option除非你做:

    option = option.replace(guess, "_");

  • 此外,由于你不想使用数组,我强烈建议你使用StringBuilder

EDIT 1(根据开来重复螺纹评论):
可以使用StringBuilder拥有初始化为全部-的字符串。然后,当有人猜测一个正确的信件时,您可以用guess替换-

StringBuilder sb_word = new StringBuilder(lengthOfOriginalString); 

for (int i = 0; i < length; i++) 
    sb_word.append('-'); //add hyphens to StringBuilder, StringBuffer would also work 

你真的应该使用这样的:

final char blank = '-'; 

然后,有人提出一个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); 
     } 
    } 
} 
+0

option = option.replace(guess,“_”); 我们还没有学习stringbuilder。 所以现在我的代码工作,如果字符猜测是错误的。任何建议如果他们猜测正确的角色怎么办? – Treasure

+0

@Treasure我很少这么做,但你看起来很失落。你必须主动为自己学习。谁在乎你没有学习'StringBuilder',去了解它。谷歌,兄弟。不要指望有人只给你代码。并非所有东西都明确存在,例如变量声明等。我会留给你弄清楚,但逻辑是正确的。祝你好运。 –

+0

@Treasure PS:如果您复制这段代码并将其作为您的作业递交:(a)您的老师或助教,或者您最有可能知道哪些不属于您的,(b)仅仅是抄袭和(c)不必像简单地改变变量名称那样做一些愚蠢的事情,它并不像人们想象的那么聪明(过去5年来一直是一个技术援助,抓住了人们一直在欺骗的人)。祝你好运。 –