将整数转换为“/ \”字符串

时间:2017-06-13 09:15:24

标签: java unit-testing

我正在尝试从“/”或“\”

转换整数到字符串构建

例如:6 = /\,22 = //\\\\  ,其中/ = 1 \ = 5

对于x = 1或x = 5是正确的

    public String fromArabic(int x)
    {
        if(x>29 || x<1)
            throw new IllegalArgumentException("Error IllegalArgumentException");

        int tmp=x;
        String out="";
        StringBuilder o=new StringBuilder(out);

        while(tmp!=0)
        {
            if(tmp-5>=0)
            {
                tmp-=5;
                o.append("\\");
            }
            else if(tmp-1>=0 && tmp-5<0)
            {
                tmp-=1;
                o.append("/");
            }
        }

       out=o.toString();
       return out;
    }

输出:

  

预期:其中[// \\&GT;但是:&lt; [\\ //]&gt;

如何使其正确?

5 个答案:

答案 0 :(得分:3)

在tmp == 0之前不需要循环,并且每次迭代减去5或1 如果您将x / 5分配给int,则会获得'\'个符号的数量, 并x % 5为您提供'/'符号数

这是一行(Java 8)

return String.join("", Collections.nCopies((x%5), "/"), Collections.nCopies((x/5), "\\"));

答案 1 :(得分:1)

public String fromArabic(int x)
{
    if(x>29 || x<1)
        throw new IllegalArgumentException();
    int tmp = x;
    StringBuilder o = new StringBuilder();
    while(tmp != 0)
    {
        if(tmp >= 5)
        {
            tmp -= 5;
            o.append("\\");
        }
        else if(tmp >= 1)
        {
            tmp-=1;
            o.append("/");
        }
    }
   return o.reverse().toString();
}

答案 2 :(得分:0)

你需要在内部反转if序列:

while(tmp!=0)
{
  if  (tmp-1>=0 && tmp-5<0)
  {
    tmp-=1;
    o.append("/");
  }
  else if(tmp-5>=0)
  {                
    tmp-=5;
     o.append("\\");
  }
}

答案 3 :(得分:0)

我建议分两个阶段构建字符串:

  1. 添加所有1-s
  2. 添加所有5-s
  3. 从技术上讲,我们只需要x % 5 /个字符,后跟x / 5 \个字符。

    像这样(如果我们想要备用循环和StringBuilder):

      // static: we don't want "this" in the context
      public static String fromArabic(int x) {
        if (x > 29 || x < 1)
          throw new IllegalArgumentException("Error IllegalArgumentException");
    
        // Let's preserve StringBuilder and the loop(s)
        StringBuilder sb = new StringBuilder();
    
        for (int i = 0; i < x % 5; ++i)
          sb.append('/');
    
        for (int i = 0; i < x / 5; ++i)
          sb.append('\\');
    
        return sb.toString(); 
      }
    

    较短的代码是直接构建字符串:

      public static String fromArabic(int x) {
          if (x > 29 || x < 1)
              throw new IllegalArgumentException("Error IllegalArgumentException");
    
          char[] chars = new char[x % 5 + x / 5];
          Arrays.fill(chars, 0, x % 5, '/');    // Ones
          Arrays.fill(chars, x / 5, '\\');      // Fives
    
          return new String(chars);
      }
    

答案 4 :(得分:0)

试试这个:

public static String fromArabic(int x) {
    if(x > 29 || x < 1)
        throw new IllegalArgumentException("Error IllegalArgumentException");

    StringBuilder out = new StringBuilder("");

    int countOf1 = x % 5;
    int countOf5  = x / 5;

    for (int i = 0; i < countOf1; i++) {
        out.append("/");
    }

    for (int i = 0; i < countOf5; i++) {
        out.append("\\");
    }

    return out.toString();
}
相关问题