2012-08-30 62 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