2012-08-30 64 views
-4

如果UD = U,杜= d,UU = d,DD = U映射字符串他人在Java中

如果我得到的輸出 「UU杜UD」 有沒有辦法讓輸出這樣

uu du ud ---Output already got 

d d u ---Because uu=d du=d ud=u 

    u u ---Because dd=u and the other u comes down 

    d  ---finally uu=d so the output is d. 

我得到這種類型的輸出有問題,因爲我不知道java很多。 我寫的代碼是

public class Test 
{ 
    public static void main(String[] args) 
    { 

    int count = 0; 
    int index = 0; 
    Scanner scan = new Scanner(System.in); 

    System.out.print("How many Line Paremeters?"); 
    int amount = scan.nextInt(); 

    // One array to hold all the names 
    String[] name = new String[amount]; 

    System.out.print("You entered " 
     + amount + " as the size of your name list."); 
    System.out.println(" "); 

    // Ask for all the names 
    for (index = 0; index < amount; index++) 
    { 
     System.out.print("Enter Line Paremeters: "); 
     name[index] = scan.next(); 
    } 

    System.out.println(" "); 
    System.out.println("The order: "); 
    System.out.println(" "); 
    System.out.println(); 

    for (String names1 : name) 
    { 
     System.out.print(names1 + " "); 
    } 
    } 
} 
+3

這功課嗎? –

+6

標題,描述和代碼是否有任何關係? – GriffeyDog

+0

稍微的上下文可能會有所幫助...... –

回答

0

如果我理解正確的話,你已經有了字符串 「UU杜UD」,並要 「減少」 它的基礎上的規則:UD = U,杜= d ,uu = d,dd = u,並且任何剩下的字符將在您放置時「下降」。要做到這一點,你需要編寫一個遞歸函數或迭代循環(按照註釋)來檢查字符串中的2個字符長的子字符串,並根據您的規則進行替換。 @DuncanJones問道,我不願意幫助任何人,因爲這看起來像是作業。

+1

我懷疑迭代可能更簡單。 OP的例子似乎是以寬度優先的方式應用重寫...... –

+0

@StephenC我同意你的觀點迭代會更簡單,它看起來像一個「如何編寫遞歸函數」類型的作業,因此建議,但它也可以做簡單的迭代,所以我會相應地修改我的帖子。 – JTMon

-1

字符串將使用Java中的equals方法比較相等:

string1.equals(字符串2)

如果要比較一個常量字符串,總是將常量字符串放在左側,這樣就不需要檢查Null。

「ABC」 .equals(STRING3)

+0

可以請您詳細說明嗎? – user1630061

+1

我不明白這是如何與問題相關的... –

+0

那麼,我會直接顯示它基於您的代碼。但我發現沒有字符串比較。 – wollud1969