Aleksei Moshkov Aleksei Moshkov - 1 year ago 89
Java Question

Couldn't get correct Output

You give your guess and programm compare the result with random word from the list. If the the programm could find equal symbouls, there will be shown, if not you will see "#"-symbol. Now, I can't understand why "#" didn't dysplayed. Here is a full code.


Example:



  • apple – random word

  • apricot - answer of customer

  • ap############# (15 syllables, because of customer don't have to know
    the lenght of word)

    import 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());
    }
    }


Answer Source

It is a silly mistake you made. You never enter while because finish = true at the start.

Do this,

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

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

}

Or,

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

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

}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download