用位置替换破折号,然后存储

时间:2013-05-04 16:50:43

标签: java arrays storing-data

我正在开发一个刽子手应用程序。我现在正在编程并且正在尝试使用用户输入(tW)按位置(pos)替换input中的短划线然后存储数组...我可以找不到问题,它让我疯了!请有人帮帮我吗?

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);


}

3 个答案:

答案 0 :(得分:0)

问题在于您尝试从输入的字母pos中检索位置String而不是从阵列中选定的String检索字符。取代

char ch = input.charAt(pos);

char ch = s.charAt(pos);

答案 1 :(得分: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();

    }
}

随意将此解决方案与原始代码进行比较并进一步改进; - )

答案 2 :(得分: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;
    }
}