结果在Java代码中的格式不正确

结果在Java代码中的格式不正确,java,string,algorithm,stringbuilder,system.out,Java,String,Algorithm,Stringbuilder,System.out,在这里,我希望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 T

在这里,我希望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;
  }

}
import java.util.*;
类TestClass{
公共静态void main(字符串args[])引发异常{
StringBuilder sb=新的StringBuilder();
扫描仪扫描=新扫描仪(System.in);
int D=scan.nextInt();
int x=0,y=0,k=0;
List commondivisior=new ArrayList();
而(D-->0){
公约数;
x=scan.nextInt();
y=scan.nextInt();
k=scan.nextInt();
k=k-1;
Commondivisior=GetCommondivisior(x,y);
集合。反向(公约数);
if(k>=(commondivisior.size()){
某人追加(“\n今天没有糖果”);

}否则,如果(k您在每个循环迭代中打印字符串生成器,那么您将打印所有步骤。由于您希望避免在循环中打印,而是创建一个在最后打印的字符串,您应该将打印语句移到循环之外:

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);
while(D-->0){
公约数;
x=scan.nextInt();
y=scan.nextInt();
k=scan.nextInt();
k=k-1;
Commondivisior=GetCommondivisior(x,y);
集合。反向(公约数);
如果(k>=commondivisior.size()){
某人追加(“\n今天没有糖果”);

}否则,如果(k您在每个循环迭代中打印字符串生成器,那么您将打印所有步骤。由于您希望避免在循环中打印,而是创建一个在最后打印的字符串,您应该将打印语句移到循环之外:

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);
while(D-->0){
公约数;
x=scan.nextInt();
y=scan.nextInt();
k=scan.nextInt();
k=k-1;
Commondivisior=GetCommondivisior(x,y);
集合。反向(公约数);
如果(k>=commondivisior.size()){
某人追加(“\n今天没有糖果”);

}否则,如果(k您需要在测试用例完成后清除stringBuilder,或者您应该在处理完所有测试用例后打印。您已经持有该文件,并在每个测试用例后在while循环中打印它。因此,有两种方法可以解决此问题:

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


您需要在测试用例完成后清除stringBuilder,或者在处理完所有测试用例后打印。您已经保留了该选项,并在每个测试用例后在while循环中打印它。因此,有两种方法可以解决此问题:

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


在循环的时候把这行写在外面。剩下的看起来没问题

         System.out.println(sb);

在循环的时候把这行写在外面。剩下的看起来没问题

         System.out.println(sb);

写入
System.out.println(sb)
之后循环
并尝试…谢谢将其作为我将接受的答案写下来。注意,对于这样的琐碎连接,这并不重要。您是否遇到实际性能问题?是的。我遇到了,甚至在使用StringBuider之后,它也没有得到解决。write
System.out.println(sb)
之后循环
并尝试…谢谢将其写为我将接受的答案。请注意,对于这样的琐碎连接,这并不重要。您是否遇到实际性能问题?是的。我遇到了,甚至在使用StringBuider之后,它也没有得到解决。如何初始化StringBuilder。请参阅我使用n的方式ew运算符。如何初始化StringBuilder。请参见我使用新运算符的方式。