在学校的java中编写一个冒泡排序算法并随机地在屏幕上打印一条额外的行,我无法弄清楚它为什么会这样做。这是我的代码。Code result here
import java.util.Scanner;
public class Assignment7 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter the # of numbers to be sorted: ");
int number = input.nextInt();
System.out.print("Enter 1 for ints or 2 for doubles: ");
int intdou = input.nextInt();
if (intdou == 1) {
int x = 0;
int[] num = new int[number];
for (int q = 0; q < number; q++) {
System.out.print("Enter number: ");
num[q] = input.nextInt();
}
for (int w = 0; w < number; w++) {
for (int e = 1; e < (number - w); e++) {
if (num[e - 1] > num[e]) {
x = num[e - 1];
num[e - 1] = num[e];
num[e] = x;
for (int r = 0; r < number; r++) {
System.out.print(num[r] + ", ");
}
}
System.out.print("\n");
}
}
} else if (intdou == 2) {
double x = 0;
double[] num = new double[number];
for (int q = 0; q < number; q++) {
System.out.print("Enter number: ");
num[q] = input.nextInt();
}
for (int w = 0; w < number; w++) {
for (int e = 1; e < (number - w); e++) {
if (num[e - 1] > num[e]) {
x = num[e - 1];
num[e - 1] = num[e];
num[e] = x;
for (int r = 0; r < number; r++) {
System.out.print(num[r] + ", ");
}
}
System.out.print("\n");
}
}
}
}
}
答案 0 :(得分:1)
你的意思是多余的空白行?只需移动“System.out.print(”\ n“);”在if块内。