如何在内部循环之前完全打印外部循环(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("ttttttDistance To (Kilometers)");
System.out.println("Enemiest  X Coord  tY Coord  tE-0tE-1tE-2tE-3tE-4tE-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("%.2ft", distance);  
}
// the following code below is somehow printed on the far right 
System.out.println("E- " + i + "tt  " + xCoords[i] + "t      " + yCoords[i]); 
}




for (int i = 0; i < 6; i++) {
System.out.printf("E-" + i + "tt" + xCoords[i] + "tt" + yCoords[i] + "tt");
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("%.2ft", distance);
}
System.out.println();
}

相关内容

  • 没有找到相关文章

最新更新