【问题标题】:The Result is not coming in correct format in Java codeJava 代码中的结果格式不正确
【发布时间】:2015-05-17 12:30:45
【问题描述】:

在这里,我希望 Stringbuilder 附加每个计算的结果。 但它的格式不正确。

在代码中,我试图找到两个数字的所有命令因子并获取第 k 个值。但是我的结果不是预期的格式。

Expected Correct Output
4
1
No candy today

我得到的是这个:-

4

4
1

4
1

No candy today

我需要使用 StringBuilder 来快速处理输出。而不是每次只需要调用一次 System.out。

import java.util.*;

class TestClass {
    public static void main(String args[] ) throws Exception {
        StringBuilder sb = new StringBuilder();
        Scanner scan = new Scanner(System.in);
        int D = scan.nextInt();
        int x = 0, y = 0, k = 0;
        List<Integer> commonDivisor = new ArrayList<Integer>();
        while(D-->0){
            commonDivisor.clear();
            x = scan.nextInt();
            y = scan.nextInt();
            k = scan.nextInt();
            k = k - 1;

            commonDivisor = getCommonDivisor(x, y);
            Collections.reverse(commonDivisor);
            if(k >= (commonDivisor.size())){
              sb.append("\nNo candy today");
            }else if(k <= (commonDivisor.size() - 1)){
                 sb.append(commonDivisor.get(k) + "\n");
            }
            System.out.println(sb);
        }
    }

    public static List<Integer> getCommonDivisor(int num1, int num2) {

    List<Integer> list = new ArrayList<Integer>();

    int min = minimum(num1, num2);

    for(int i = 1; i <= min / 2; i++) { 
        if (num1 % i == 0 && num2 % i == 0) {
            list.add(i);
        }
    }

    if (num1 % min == 0 && num2 % min == 0) {
        list.add(min);
    }

    return list;
  }

  public static int minimum(int num1, int num2) {
    return num1 <= num2 ? num1 : num2;
  }

}

【问题讨论】:

  • while loop 之后写System.out.println(sb); 并尝试...
  • 谢谢把它写成我会接受的答案。
  • 请注意,对于像这样的简单连接,它并不重要。您是否遇到实际性能问题?
  • 是的。我正在经历,即使在使用 StringBuider 之后也没有得到解决。

标签: java string algorithm stringbuilder system.out


【解决方案1】:

您在每次循环迭代中打印字符串生成器,因此您打印所有步骤。由于您想避免在循环内打印,而是构建一个在最后打印的字符串,您应该将 print 语句移到循环外:

while(D-->0){
    commonDivisor.clear();
    x = scan.nextInt();
    y = scan.nextInt();
    k = scan.nextInt();
    k = k - 1;

    commonDivisor = getCommonDivisor(x, y);
    Collections.reverse(commonDivisor);
    if (k >= commonDivisor.size()) {
       sb.append("\nNo candy today");
    } else if (k <= (commonDivisor.size() - 1)){
       sb.append(commonDivisor.get(k) + "\n");
    }
}
System.out.println(sb);

【讨论】:

    【解决方案2】:

    您需要在完成测试用例后清除您的 stringBuilder,或者您应该在处理完所有测试用例后打印。您已经持有它并在每个测试用例之后在 while 循环中打印它。所以有两种方法可以解决这个问题:

    }//while loop ends
    System.out.println(sb);//this is what i would prefer
    

    或者

    while(D-->0){
         sb = new StringBuilder();
         ...    
    

    【讨论】:

    • 如何初始化 StringBuilder。
    • 看看我使用 new 运算符的方式。
    【解决方案3】:

    在while循环之外写下这一行。休息似乎还可以。

             System.out.println(sb);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-22
      • 1970-01-01
      相关资源
      最近更新 更多