Java 控制台输出未在下一行打印
Java Console Output Not Printing on The Next Line
你好,我开始在这里做一些关于 dmoj link 的问题: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
请帮忙,我是初学者,我在基本输入输出方面遇到问题谢谢!
我假设您是 copying/pasting 您的输入,否则它看起来像这样:
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
你好,我开始在这里做一些关于 dmoj link 的问题: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
请帮忙,我是初学者,我在基本输入输出方面遇到问题谢谢!
我假设您是 copying/pasting 您的输入,否则它看起来像这样:
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