如何在内循环之前完全打印外循环 (Java)
How to fully print outer loop before inner loop (Java)
我的目标是打印一个图表,显示“敌人”编号、x 和 y 坐标,然后是它们相对于其他“敌人”的距离,但下面是我的嵌套循环、敌人编号和坐标与距离数字相反。我试过调整代码的位置,但似乎没有解决问题。
public static void main(String[] args) {
// array of enemy coordinates
int[] xCoords = {1, 4, 5, 5, 7, 10};
int[] yCoords = {3, 4, 6, 2, 5, 2};
System.out.println("________Enemy Distance Chart________");
System.out.println("\t\t\t\t\t\tDistance To (Kilometers)");
System.out.println("Enemies\t X Coord \tY Coord \tE-0\tE-1\tE-2\tE-3\tE-4\tE-5");
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
double distance = Math.sqrt((yCoords[j]-yCoords[i])*(yCoords[j]-yCoords[i]) + (xCoords[j]-xCoords[i])*(xCoords[j]-xCoords[i]));
System.out.printf("%.2f\t", distance);
}
// the following code below is somehow printed on the far right
System.out.println("E- " + i + "\t\t " + xCoords[i] + "\t " + yCoords[i]);
}
for (int i = 0; i < 6; i++) {
System.out.printf("E-" + i + "\t\t" + xCoords[i] + "\t\t" + yCoords[i] + "\t\t");
for (int j = 0; j < 6; j++) {
double distance = Math.sqrt((yCoords[j] - yCoords[i]) * (yCoords[j] - yCoords[i]) + (xCoords[j] - xCoords[i]) * (xCoords[j] - xCoords[i]));
System.out.printf("%.2f\t", distance);
}
System.out.println();
}
我的目标是打印一个图表,显示“敌人”编号、x 和 y 坐标,然后是它们相对于其他“敌人”的距离,但下面是我的嵌套循环、敌人编号和坐标与距离数字相反。我试过调整代码的位置,但似乎没有解决问题。
public static void main(String[] args) {
// array of enemy coordinates
int[] xCoords = {1, 4, 5, 5, 7, 10};
int[] yCoords = {3, 4, 6, 2, 5, 2};
System.out.println("________Enemy Distance Chart________");
System.out.println("\t\t\t\t\t\tDistance To (Kilometers)");
System.out.println("Enemies\t X Coord \tY Coord \tE-0\tE-1\tE-2\tE-3\tE-4\tE-5");
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
double distance = Math.sqrt((yCoords[j]-yCoords[i])*(yCoords[j]-yCoords[i]) + (xCoords[j]-xCoords[i])*(xCoords[j]-xCoords[i]));
System.out.printf("%.2f\t", distance);
}
// the following code below is somehow printed on the far right
System.out.println("E- " + i + "\t\t " + xCoords[i] + "\t " + yCoords[i]);
}
for (int i = 0; i < 6; i++) {
System.out.printf("E-" + i + "\t\t" + xCoords[i] + "\t\t" + yCoords[i] + "\t\t");
for (int j = 0; j < 6; j++) {
double distance = Math.sqrt((yCoords[j] - yCoords[i]) * (yCoords[j] - yCoords[i]) + (xCoords[j] - xCoords[i]) * (xCoords[j] - xCoords[i]));
System.out.printf("%.2f\t", distance);
}
System.out.println();
}