簡體   English   中英

在文字游戲中顯示字母

[英]Revealing a letter in a word game

我正在做文字游戲作為家庭作業。 但是我被困住了。 我有一個打亂的單詞,但是我需要每回合顯示出它的第一個字母(然后是第2、3rd ...)。 我試圖做這樣的事情:

char[] arr = a.toCharArray();
   for ( int j = 0; j<arr.length; j++) {

          if (original[j] == shuffled[j] ) { //If the not shuffled word's first letter equals to shuffled word's first letter then move to the 2nd letter and reveal it. Maybe update the j to j+1?}

          else {
            char temp = shuffled[j];
            shuffled[j] = original[j];
            original[j] = temp;
            String h = new String(shuffled);
            System.out.println("test " + h) ; 

          }
        }

我的輸出應該是這樣的:

原始詞:Badger混洗詞:drBage

第一回合:Brdage

第2回合:Badrge

第三回合:Badegr

第四回合:Bad

我當前的輸出是:

原始詞:Cat123

塞12Cta3

測試C2Cta3

測試CaCta3

測試Catta3

測試Cat1a3

測試Cat123

測試Cat123

首先,不要打亂單詞-您會失去正確的字母順序。

代替:

  • 遍歷字母
  • 打印原始單詞的前“ n”個字母
  • 從n +到結尾復制數組
  • 隨機播放然后打印

以下偽代碼應有助於闡明您的問題。 我會調用一個函數,例如在每次旋轉后顯示下面定義的顯示字母。

   public class Unscramble{

    public static void main(String[] args){

        char[] orig = args[0].toCharArray();
        char[] shuffled = args[1].toCharArray(); 
        int count = 0;

        while(count < orig.length){

            if(!(orig[count] == shuffled[count]))
                shuffled = revealLetter(shuffled,orig, count);

            count++;
            System.out.println(count + " " + new String(shuffled));
        }
    }


    /*
    * Reveal letter
    * @param shuffled the shuffled array
    * @param original original char array
    * @param index index of letter to reveal
    */
    public static char[] revealLetter(char[] shuffled, char[] original, int index){
      shuffled[index] = original[index];
      return shuffled;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM