Java控制台输出不换行。

huangapple go评论80阅读模式
英文:

Java Console Output Not Printing on The Next Line

问题

以下是已翻译的内容:

你好,我开始在 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("最小周长为 " + perimeter + ",尺寸为 " + x  + " x " + y);
            }else if(y < x){
                result.append("最小周长为 " + perimeter + ",尺寸为 " + y  + " x " + x);
            }else{
                result.append("最小周长为 " + perimeter + ",尺寸为 " + y  + " x " + x);
            }
            System.out.println(result);
            result.setLength(0);
        }
    }
}

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

100
15
195
最小周长为 40,尺寸为 10 x 10 // 为什么我的输出与输入在同一行上?
最小周长为 16,尺寸为 3 x 5
最小周长为 56,尺寸为 13 x 15
请帮帮我,我是个初学者,对基本的输入输出有困扰,谢谢!
<details>
<summary>英文:</summary>
Hello I am beginning to do some questions on dmoj link here: https://dmoj.ca/problem/ccc03j2 
I have written the code: 
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 &lt; y){
result.append(&quot;Minimum perimeter is &quot; +  perimeter + &quot; with dimensions &quot; + x  + &quot; x &quot; + y);
}else if(y &lt; x){
result.append(&quot;Minimum perimeter is &quot; +  perimeter + &quot; with dimensions &quot; + y  + &quot; x &quot; + x);
}else{
result.append(&quot;Minimum perimeter is &quot; +  perimeter + &quot; with dimensions &quot; + y  + &quot; x &quot; + x);
}
System.out.println(result);
result.setLength(0);
}
}
}
but when it prints to the console it results in this:
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
Please help, I am a beginner and im having trouble with the basic input output thanks!
</details>
# 答案1
**得分**: 1
我假设您正在复制/粘贴您的输入,因为否则它会看起来像这样:

100
最小周长为40,尺寸为10 x 10
15
最小周长为16,尺寸为3 x 5
195
最小周长为56,尺寸为13 x 15
0

进程以退出代码0结束


发生的情况是,由于您的程序在读取每一行后输出,它将在粘贴输入后开始处理,每行一个条目。
在0之后包含一个换行,它将防止它们出现在同一行上。

100
15
195
0
最小周长为40,尺寸为10 x 10
最小周长为16,尺寸为3 x 5
最小周长为56,尺寸为13 x 15

进程以退出代码0结束


<details>
<summary>英文:</summary>
I&#39;m assuming you are copying/pasting your input, since otherwise it would look like this:

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


What happens is that, since your program outputs after each line read, it will start processing once the input is pasted, one entry for each line.  
Include a line feed after 0 and it will prevent them from being on the same line.

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


</details>

huangapple
  • 本文由 发表于 2020年9月4日 00:03:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63727531.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定