Merhaba arkadaşlar,
java bilgim biraz var ne yazık ki bu problem için yeterli olmadı bir arkadaşımında ödeviymiş bu program. Programda bir sorun var doğru bir harf bulunca "-" bulunan harfle değiştirmek yerine o hariç tüm harfleri açıyor ilk sorun bu. İkinciside yeniden oynama fonksiyonu çalışmıyormuş yardımcı olabilecek sorunları düzeltebilecek varsa çok sevinirim...
import java.util.*;
public class FinalProject {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
String guess, choice;
String goodguess= "_";
boolean option = true;
Random rand = new Random();
String [] Words = {"hat", "cake", "bake", "cat", "mat", "swing", "king"};
String RandomWord = Words[rand.nextInt(Words.length)];
int lengthW = RandomWord.length();
String letters [] = new String [lengthW];
String [] templateArray = new String [letters.length];
int i = 0;
System.out.println();
System.out.println("Welcome to the HANGMAN game!");
System.out.println("You have 6 guesses to find the secret word");
System.out.println("Your secret word has "+lengthW+" letters ");
System.out.println();
for (int t =0; t < lengthW ; t++) {
System.out.print(" _");
}
System.out.println();
System.out.println();
int tries = 6;
for (int t =0; t < lengthW ; t++) {
while(i<tries){
i++;
System.out.print("Please enter a letter (lower-case only) : Guess #"+i+": ");
guess = input.next();
if (RandomWord.contains(guess)){
System.out.println("Good guess!");
System.out.println(RandomWord.replaceAll(guess, goodguess));
}
else {
System.out.println("Bad guess!");
}
if (RandomWord.equals(guess)){
System.out.println("YOU WON!");
}
}
}
/*System.out.println();
System.out.println();
System.out.println("********YOU LOST*********");
System.out.println();
System.out.println();
*/
System.out.println("The secret word was:"+ " " +RandomWord);
System.out.print("Play again? (y/n): ");
choice = input.next();
if (choice.equals("y")){
option = true;
}
if (choice.equals("n")){
option = false;
System.out.println("Goodbye!");
}
}
}