【问题标题】:How to add separators between words?如何在单词之间添加分隔符?
【发布时间】:2012-10-14 20:32:19
【问题描述】:

我正在为我的作业编写一个刽子手程序,但遇到了一个小问题。 在我的程序中,为了分隔单词,我使用'|'象征。 例如,这是要猜的词:

U N I T E D | S T A T E S | O F | A M E R I C A

我有一个显示编号的 StringBuffer 变量。破折号 这里,

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

我尝试使用以下代码插入分隔符'|'

if(guessword.indexOf('|') == -1)
    System.out.print(" ");
else
    guessit.setCharAt(guessword.indexOf('|'),'|');

我知道它错了,它只插入了 1 个分隔符。 您能帮我在正确的位置插入分隔符吗? 您也可以尝试删除 System.out.print(" ");因为它不必要地留下空间。 我可以使用休息吗?反而 对于上面的例子,我得到的显示为

_ _ _ _ _ _ | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

我真的想要

_ _ _ _ _ _ | _ _ _ _ _ _ | _ _ | _ _ _ _ _ _ _

我的主要方法如下

public static void main()throws IOException
{
    Hangman obj = new Hangman();
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);
    PrintWriter p = new PrintWriter(System.out,true);

    p.println("Lets play HANGMAN");
    p.println();
    p.println("Enter your choice according to the following options.\n
               NOTE: Words are related to the topics given below.\n
               1. Sports\n2. Movies\n3. Computer\n4. Food\n5. Countries");
    p.println();
    int choice = Integer.parseInt(br.readLine());
    obj.clearScreen();
    String bothwordandclue[] = new String[2];

    if(choice == 1)
        bothwordandclue = obj.Sports();
    else if(choice == 2)
        bothwordandclue = obj.Movies();
    else if(choice == 3)
        bothwordandclue = obj.Computers();
    else if(choice == 4)
        bothwordandclue = obj.Food();
    else if(choice == 5)
        bothwordandclue = obj.Countries();
    else
        p.println("Wrong choice");

    int counter = 6;
    String guessword = bothwordandclue[0];
    String wordclue = bothwordandclue[1];

    int lengthofword = (int)(Math.round(((double)guessword.length()/2)));
    int checkguess = 0;
    String a;
    StringBuffer guessit = new StringBuffer();

    for (int i = 0;i < lengthofword; i++)
        guessit = guessit.append("_ ");

    guessit.delete((2 * lengthofword)-1,(2 * lengthofword));

    if(guessword.indexOf('|') == -1)
        System.out.print(" ");
    else
        guessit.setCharAt(guessword.indexOf('|'),'|');

    p.println(guessit);
    do
    {
        p.println();
        p.println("Enter your guess letter in capitals");
        String guessletter = br.readLine();
        obj.clearScreen();

        for(int i = 0;i<lengthofword;i++)
        {
            if(guessletter.charAt(0) == guessword.charAt(2*i))
            {
                guessit.setCharAt(2*i,guessletter.charAt(0));
                checkguess=1;
            }                
        }
        if(checkguess == 1)
        {
            p.println("Correct Guess");
            p.println(guessit);
        }
        else
        {
            counter--;
            p.println("Wrong guess. You have " + counter + 
                                    " incorrect guesses left");
            p.println(guessit);
        }
        checkguess = 0;
        a = guessit.toString();
        if(a.equals(guessword))
        {
            p.println("You guessed the word!!!!!");
            counter=0;
        }        
    }while(counter>0);
}

【问题讨论】:

    标签: java bluej


    【解决方案1】:

    您只能使用indexOf 进行操作。只是您需要使用该方法的另一个版本,它从开始搜索的位置获取位置。请参阅此处需要的String#indexOf(String str, int fromIndex)

    int index = guessword.indexOf("|");
    while(index >= 0) {
       guessIt.setCharAt(index, '|')
    
       // Start searching for next "|" after this index
       index = guessword.indexOf("|", index+1));  
    }
    

    【讨论】:

    • 赞成,因为它比我的更有效地解决了问题,但对于初学者来说并不那么明显。也许包括指向docs.oracle.com/javase/1.4.2/docs/api/java/lang/…的链接,int)
    • 非常感谢。这非常有效地解决了问题。
    • @user1772079。不客气:)。您需要通过单击答案旁边的箭头将此答案标记为已接受。
    【解决方案2】:

    问题在于setCharAtindexOf 仅适用于每个索引,这就是它仅替换第一个| 的原因。您将不得不多次遍历guessword。您可以使用 Java 的其他一些内置 String 函数或使用简单的 for 循环,即

    for(int i = 0; i < guessword.length(); i++){
        if(guessword.charAt(i) == '|')
            guessit.setCharAt(i, '|');
    }
    

    【讨论】:

    • 感谢您的帖子。在发布问题之前我已经尝试过了,它甚至没有在单词之间放置一个分隔符:D
    【解决方案3】:

    【讨论】:

    • 效率更高,但我不喜欢告诉初学者学习正则表达式的想法。
    • 这里不需要它们。为什么你认为正则表达式会起作用?我们正在根据其他字符串替换字符串中的字符
    • @PWhite 如果是为了分配,我想他有一天会学习正则表达式,那为什么不今天呢?
    • @Rohit Jain 我没有回答他的精确用例,但提出了一个学习建议,这将简化他的算法并可能给他一个更好的分数;)
    • 感谢您的帖子。我要学习所有这些复杂的东西还有很长的路要走。我大约一年前在学校开始学习 Java,对这一切一无所知,但我一定会学习