java控制台输出不在下一行打印

5lwkijsr  于 2021-07-05  发布在  Java
关注(0)|答案(1)|浏览(547)

你好,我开始在这里问一些关于dmoj链接的问题:https://dmoj.ca/problem/ccc03j2
我已经写了代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Solution {
    public static void main(String args[]) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int x = 0;
        int y = 0;
        boolean loop = true;
        StringBuilder result = new StringBuilder();
        while(loop){
            int pictures = Integer.parseInt(br.readLine());
            if(pictures == 0){
                break;
            }
            int max = (int) Math.ceil(Math.sqrt(pictures));
            int min = (int) Math.floor(Math.sqrt(pictures));
            if(pictures % max == 0){
                x = max;
                y = pictures / max;
            }
            else if(pictures % min == 0){
                x = min;
                y = pictures / min;
            }
            int perimeter = ((x+y) / 2) * 4;
            if(x < y){
                result.append("Minimum perimeter is " +  perimeter + " with dimensions " + x  + " x " + y);
            }else if(y < x){
                result.append("Minimum perimeter is " +  perimeter + " with dimensions " + y  + " x " + x);
            }else{
                result.append("Minimum perimeter is " +  perimeter + " with dimensions " + y  + " x " + x);
            }
            System.out.println(result);
            result.setLength(0);
        }
    }
}

但当它打印到控制台时,结果是:

100
15
195
0Minimum perimeter is 40 with dimensions 10 x 10 //Why is my output on the same line as my input?
Minimum perimeter is 16 with dimensions 3 x 5
Minimum perimeter is 56 with dimensions 13 x 15

请帮助,我是一个初学者,我有麻烦的基本输入输出谢谢!

nwlqm0z1

nwlqm0z11#

我假设您正在复制/粘贴您的输入,否则它将如下所示:

100
Minimum perimeter is 40 with dimensions 10 x 10
15
Minimum perimeter is 16 with dimensions 3 x 5
195
Minimum perimeter is 56 with dimensions 13 x 15
0

Process finished with exit code 0

所发生的情况是,由于您的程序在每一行读取后都会输出,因此一旦粘贴了输入,它就会开始处理,每一行对应一个条目。
在0之后包含换行符,这样可以防止它们在同一行上。

100
15
195
0
Minimum perimeter is 40 with dimensions 10 x 10
Minimum perimeter is 16 with dimensions 3 x 5
Minimum perimeter is 56 with dimensions 13 x 15

Process finished with exit code 0

相关问题