2017-05-10 3 views
-2

Sie geben Ihre Schätzung und Programm vergleichen das Ergebnis mit zufälligem Wort aus der Liste. Wenn das Programm gleiche Symbole finden kann, wird dort angezeigt, wenn nicht, wird das "#" - Symbol angezeigt. Jetzt kann ich nicht verstehen, warum "#" nicht gespielt hat. Hier ist ein vollständiger Code.Konnte nicht korrekt ausgegeben werden

Beispiel:

  • apple - Zufallswort
  • Aprikose - Antwort von Kunden
  • ap ############# (15 Silben, wegen des Kunden müssen nicht wissen die Länge des Wortes)

    importieren java.util.Random; Import java.util.Scanner;

    public class Main { 
        private static Scanner scan = new Scanner(System.in); 
    
        public static final int N = 30; 
    
        public static void main(String[] args) { 
         String ranWord; 
         Random rand = new Random(); 
    
         String[] words = new String[N]; 
         words[0] = "appricot"; 
         words[1] = "orange"; 
         words[2] = "cucumber"; 
         words[3] = "potato"; 
         words[4] = "tomato"; 
         words[5] = "cherry"; 
         words[6] = "banana"; 
         words[7] = "carrot"; 
         words[8] = "were"; 
         words[10] = "very"; 
         words[11] = "tasty"; 
         words[12] = "as"; 
         words[13] = "usual"; 
         words[14] = "and"; 
         words[15] = "fresh"; 
         words[16] = "and"; 
         words[17] = "tasty"; 
         words[18] = "passed"; 
         words[19] = "for"; 
         words[20] = "cooking"; 
         words[21] = "a"; 
         words[22] = "chicken"; 
         words[23] = "it"; 
         words[24] = "isn't"; 
         words[25] = "necessary"; 
         words[26] = "cook"; 
         words[27] = "chicken"; 
         words[28] = "every"; 
         words[29] = "day"; 
    
         System.out.println("Try to guess the word, call Your variant?" + "\n"); 
         ranWord = words[rand.nextInt(N)]; 
         System.out.println("Computer guess the word: " + ranWord); 
    
         Computer computer = new Computer(ranWord); 
    
         String customWord = scan.nextLine(); 
         Customer customer = new Customer(customWord); 
    
         boolean finish = true; 
         while (!finish) { 
          //customWord = scan.nextLine(); 
          if (customer.word.equals(computer.ranWord)) { 
           System.out.println("Succsesful prompt!"); 
           finish = true; 
          } else { 
           checkIsFinish(customWord, ranWord); 
           finish = false; 
          } 
         } 
        } 
    
        static void checkIsFinish(String customWord, String ranWord) { 
         int minLenghtWord = customWord.length() < ranWord.length() ? customWord.length() : ranWord.length(); 
    
         for (int i = 0; i < minLenghtWord; i++) { 
          if (customWord.charAt(i) == ranWord.charAt(i)) { 
           System.out.print(ranWord.charAt(i)); 
          } else { 
           System.out.print("#"); 
          } 
         } 
         for (int i = 0; i < 15 - minLenghtWord; i++) { 
          System.out.print("#"); 
         } 
         System.out.println(customWord.length()); 
        } 
        } 
    
+0

Sie drucke sie nicht in 1 Zeile, teilen Sie es in 15 Zeilen mit 'System.out.println' für 1 Zeile ohne Pausen verwenden 'System.out.print' – XtremeBaumer

+0

Es spielt keine Rolle. Ich habe "drucken" verwendet. Ich kann das Ergebnis der Methode checkIsFinish nicht sehen. Ich verstehe nicht, wenn das Programm es –

+1

verwenden scheint das Problem woanders zu sein. Ihre Methode 'checkisFinish' funktioniert gut. ich probierte mit 'checkIsFinish (" apricot "," apple ");' und bekam die Ausgabe: 'ap ############# 7' – XtremeBaumer

Antwort

1

Es ist ein dummer Fehler, den Sie gemacht. Sie geben niemals while ein, da finish = true am Anfang steht.

dazu

finish = true; 
while (finish) { 
    //customWord = scan.nextLine(); 
    if (customer.word.equals(computer.ranWord)) { 
     System.out.println("Succsesful prompt!"); 

    } else { 
     checkIsFinish(customWord, ranWord); 
    } 
    finish = false; 

} 

Oder

finish = false; 
while (!finish) { 
    //customWord = scan.nextLine(); 
    if (customWord.equals(ranWord)) { 
     System.out.println("Succsesful prompt!"); 

    } else { 
     checkIsFinish(customWord, ranWord); 
    } 
    finish = true; 

} 
Verwandte Themen