2016-08-16 16 views
-3

私は単語ゲームのJavaコードを推測するのに問題があります。 私はあなたに私の現在のコードを表示して問題を解決します。あなたは、行番号39で見ることができるようJavaの単語ゲームを推測します。

import java.util.Scanner; 

class Q 
{ 
    public static void main(String argss[]) 
    { 
     String strs[] = { "kareem", "java", "izz", "tamtam", "anas" }; 
     Scanner input = new Scanner(System.in); 
     int index = ((int) (Math.random() * 5)); 
     int points = 50; 
     String c; 
     boolean result = false; 
     boolean finalResult = false; 
     boolean tempResult = false; 

     String word = strs[index]; 
     System.out.println(
       "\t *** Enter a character and guess the world*** \n\t ***You will loose if your points become 0 *** \n "); 
     System.out.println(stars(word)); 
     System.out.println("Your points: " + points); 
     System.out.print("Enter and guess a character! "); 

     String temp = stars(word); 
     String oldTemp = temp; 

     c = input.next(); 
     while (points > 0) 
     { 

      for (int i = 0; i < word.length(); i++) 
      { 
       result = (word.charAt(i) + "").equals(c); 
       if (result == true) 
       { 
        temp = toChar(i, temp, c); 
       } 

      } 

      finalResult = temp.equals(word); 
      tempResult = temp.equals(oldTemp); 
      System.out.println(temp); 
      if (finalResult == true) 
      { 
       System.out.println("\n \n YOU HAVE GUESSED THE WORLD,YOU WON ! "); 
       break; 
      } 
      if (tempResult == true) 
      { 
       points = points - 5; 
       System.out.println("False and now your points are " + points); 
      } 

      else if (tempResult == false) 
      { 
       System.out.println("True and now your points are " + points); 

      } 
      if (points <= 0) 
      { 
       System.out.println("\n\n*********************\n* Sorry , you loose *\n********************* "); 
       break; 
      } 
      System.out.print("Guess another time,enter a character: "); 
      c = input.next(); 
     } 

    } 

    public static String stars(String word) 
    { 
     String temp = ""; 
     for (int i = 0; i < word.length(); i++) 
      temp = temp + "*"; 
     return temp; 
    } 

    public static String toChar(int index, String temp, String c) 
    { 
     char[] tempChar = temp.toCharArray(); 
     char s = c.charAt(0); 
     tempChar[index] = s; 
     temp = String.valueOf(tempChar); 
     return temp; 
    } 
} 

は今、私はその偽のは、それがもはや右のだろうときので、ここで少し問題を抱えています。 エントリが正しいかどうかを比較する別の方法が分かりますか?

+1

Um、どのラインが39ですか? – bradimus

+0

@bradimus tempResult = temp.equals(oldTemp); –

+0

残念ながら、私のテレパシーは今働いていないので、私はどのラインが39番線であるかを知ることができません。私たちとIzzeddinを分かち合うことができますか? –

答えて

0

whileループ内でoldTempが変更されているように見えません。すべてのif文の後に、tempに設定してみてください。

if (points <= 0) 
{ 
    System.out.println("\n\n*********************\n* Sorry , you loose *\n********************* "); 
    break; 
} 
oldTemp = temp; // add this here 
System.out.print("Guess another time,enter a character: "); 
c = input.next();