-4

如果 ud = u , du = d , uu = d , dd = u

如果我得到输出“uu du 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 + " ");
    }
  }
}
4

2 回答 2

0

如果我理解正确,您已经有了字符串“uu du ud”,并且您想根据以下规则“减少”它:ud = u、du = d、uu = d、dd = u,任何剩余的字符都会“来”下”,正如你所说。为此,您需要编写一个递归函数或一个迭代循环(根据注释),以 2 个字符长的子字符串块检查字符串,并根据您的规则进行替换。我不愿意提供任何进一步的帮助,因为正如@DuncanJones 所问的那样,这似乎是家庭作业。

于 2012-08-30T14:09:24.123 回答
-1

将使用 Java 中的 equals 方法比较字符串是否相等:

string1.equals(string2)

如果您必须与常量字符串进行比较,请始终将常量字符串放在左侧,这样您就不需要检查 Null。

"abc".equals(string3)

于 2012-08-30T13:58:47.863 回答