我希望将用户输入的所有纬度和经度数据写入WriteGPSData.txt。但是这段代码只写了纬度和经度的最后一个输入。帮我解决这个问题。
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JOptionPane;
public class GPS_main {
public static void main(String[] args) {
String strFilePath = "WriteGPSData.txt";
try {
System.out.println("+++=====GPS POINTS=======+++");
// GPS p1 = new GPS ();
GPS[] p2;
p2 = new GPS[3];
p2[0] = new GPS();
// GPS p2 = new GPS();
for (int i = 0; i < p2.length; i++) {
p2[i] = new GPS();
String aa = JOptionPane.showInputDialog("Enter latitude: ");
double a = Double.parseDouble(aa);
p2[i].setLat(a);
String bb = JOptionPane.showInputDialog("Enter longitude: ");
double b = Double.parseDouble(bb);
p2[i].setLon(b);
// System.out.println(p2[i].toGPSString());
FileOutputStream fos = new FileOutputStream(strFilePath);
DataOutputStream dos = new DataOutputStream(fos);
String a2 = Double.toString(a);
String b2 = Double.toString(b);
// System.out.println("hello"+a2+b2);
dos.writeBytes(a2 + " " + b2);
dos.close();
}
} catch (IOException e) {
System.out.println("IOException : " + e);
// System.out.println(" ");
}
}
}
在 BlueJ 中运行