2013-05-04 55 views
2

我正在開發一個hang子手應用程序。我現在正在編程,並試圖在此刻用位置(pos)用用戶輸入(input)替換tW中的一個短劃線,然後存儲該數組...我找不到問題,並且它讓我瘋狂! !請有人幫助我?用用戶輸入替換短劃線,然後存儲

public static void main(String[] args) { 
    //get random array element 
    System.out.println("Welcome to the Hangman App"); 
    String array[] = new String[10]; 
    array[0] = "Hamlet"; 
    array[1] = "Mysts of Avalon"; 
    array[2] = "The Iliad"; 
    array[3] = "Tales from Edger Allan Poe"; 
    array[4] = "The Children of Hurin"; 
    array[5] = "The Red Badge of Courage"; 
    array[6] = "Of Mice and Men"; 
    array[7] = "Utopia"; 
    array[8] = "Chariots of the Gods"; 
    array[9] = "A Brief History of Time"; 

    ArrayList<String> list = new ArrayList<String>(Arrays.asList(array)); 
    Collections.shuffle(list); 
    String s = list.get(0); 
    //for testing 
    System.out.println(s); 


    //replace non-white space char with dashes 
    String tW = s.replaceAll("\\S", "-"); 

    //get user input 
    System.out.println("Enter an Letter: "); 
    String input = sc.next(); 

    //find position of user input and replace 
    int pos = s.indexOf(input); 
    char ch = input.charAt(pos); 
    char[] answer = tW.toCharArray(); 
    //answer[pos] = input; 

    //test 
    System.out.println(answer); 


} 
+1

不知道是否有更多的錯誤封裝它,但你怪的輸出是由於'的System.out.println(答案);'和'的System.out更換。 println(新字符串(答案));' – jlordo 2013-05-04 16:53:32

+0

這條線是幹什麼用的? char ch = input.charAt(pos); ch從未在代碼 – 2013-05-04 16:58:17

+0

中使用我認爲你沒有在這裏給出你的整個代碼。例如。當有多個角色出現時會發生什麼? – Bill 2013-05-04 17:00:05

回答

0
char inp = input.charAt(0); 
//find position of user input and replace 
char[] answer = tW.toCharArray(); 
for(int i = 0; i < answer.length; i++){ 
    if(s.charAt(i) == inp) 
     answer[i] = inp; 
} 

現在是工作,但只爲一個字符。你應該用while循環即

int numError = 0; 
int maxError = a_number; 
boolean finished = false; 
while(!finished && numError < maxError){ 
    do_all_the_things_you_are_doing_now; 

    if(ch is not exist in answer) 
     numError++; 

    else{ 
     if(there is no "_" in answer) 
      finished = true; 
    } 
} 
0

的問題是,你正試圖從進入,而不是從陣列選定StringString檢索在pos位置的字符。更換

char ch = input.charAt(pos); 

在你的代碼
char ch = s.charAt(pos); 
0

已經有幾個問題。請嘗試以下方法進行:

package com.sandbox; 

import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.Collections; 
import java.util.Scanner; 

public class Sandbox { 

    public static void main(String[] args) { 

     // get random array element 
     System.out.println("Welcome to the Hangman App"); 
     String array[] = new String[10]; 
     array[0] = "Hamlet"; 
     array[1] = "Mysts of Avalon"; 
     array[2] = "The Iliad"; 
     array[3] = "Tales from Edger Allan Poe"; 
     array[4] = "The Children of Hurin"; 
     array[5] = "The Red Badge of Courage"; 
     array[6] = "Of Mice and Men"; 
     array[7] = "Utopia"; 
     array[8] = "Chariots of the Gods"; 
     array[9] = "A Brief History of Time"; 

     ArrayList<String> list = new ArrayList<String>(Arrays.asList(array)); 
     Collections.shuffle(list); 

     String s = list.get(0); 
     // for testing 
     System.out.println(s); 

     // replace non-white space char with dashes 
     StringBuilder tW = new StringBuilder(s.replaceAll("\\S", "-")); 

     // get user input 
     System.out.println("Enter an Letter: "); 

     Scanner sc = new Scanner(System.in); 

     while (tW.indexOf("-") >= 0) { 

      String input = sc.next(); 
      char c = input.charAt(0); 

      // find position of user input and replace 
      int pos = s.indexOf(c); 

      if (pos >= 0) { 
       tW.setCharAt(pos, c); 
       System.out.println(tW.toString()); 
      } 

     } 

     sc.close(); 

    } 
} 

隨意這一解決方案進行比較,以你的原代碼,並進一步提高其;-)

相關問題