我正在开发一个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);
}
不知道是否有更多的错误封装它,但你怪的输出是由于'的System.out.println(答案);'和'的System.out更换。 println(新字符串(答案));' – jlordo 2013-05-04 16:53:32
这条线是干什么用的? char ch = input.charAt(pos); ch从未在代码 – 2013-05-04 16:58:17
中使用我认为你没有在这里给出你的整个代码。例如。当有多个角色出现时会发生什么? – Bill 2013-05-04 17:00:05