• 05-05-2014, 19:32:25
    #1
    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!");
     }
     }
    }
  • 06-05-2014, 15:16:03
    #2
    Merhaba,

    Kodu kabaca duzeltmeye calistim. Duzgun hali asagida.

    import java.util.Random;
    import java.util.Scanner;
    
    public class FinalProject {
    
    	private static final String[] WORDS = { "hat", "cake", "bake", "cat", "mat", "swing", "king" };
    
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    
    		boolean option = true;
    
    		do {
    			String guess, choice;
    
    			Random rand = new Random();
    			String randomWord = WORDS[rand.nextInt(WORDS.length)];
    			int wordLength = randomWord.length();
    
    			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 " + wordLength + " letters ");
    
    			String blankWord = initWord(randomWord);
    			print(blankWord);
    
    			System.out.println();
    
    			for (int i = 0; i < 6; i++) {
    				System.out.print("Please enter a letter (lower-case only) : Guess #" + i + ": ");
    				guess = input.next();
    
    				int index = randomWord.indexOf(guess);
    				if (index != -1) {
    					System.out.println("Good guess!");
    					blankWord = blankWord.substring(0, index) + guess + blankWord.substring(index + 1);
    					print(blankWord);
    				} else {
    					System.out.println("Bad guess!");
    				}
    
    				if (randomWord.equals(blankWord)) {
    					System.out.println();
    					System.out.println("YOU WON!");
    					break;
    				}
    			}
    
    			System.out.println("The secret word was:" + " " + randomWord);
    			System.out.println();
    
    			System.out.print("Play again? (y/n): ");
    			choice = input.next();
    			if (choice.equals("y")) {
    				option = true;
    			} else {
    				option = false;
    				System.out.println("Goodbye!");
    			}
    
    		} while (option);
    	}
    
    	private static String initWord(String word) {
    		StringBuilder sb = new StringBuilder();
    		for (int i = 0; i < word.length(); i++) {
    			sb.append("_");
    		}
    		return sb.toString();
    	}
    
    	private static void print(String word) {
    		StringBuilder sb = new StringBuilder();
    		for (int i = 0; i < word.length(); i++) {
    			sb.append(word.charAt(i)).append(" ");
    		}
    		System.out.println(sb.toString());
    	}
    }
  • 07-05-2014, 10:40:05
    #3
    Çok teşekkürler yardımın için kod dediğin gibi sorunsuz çalışıyor artık. Emeğine sağlık...


    iPad 'den Tapatalk aracılığı ile gönderildi