我的代码中的方法产生正确的输出。但是,我无法从我的方法中获取输出并将其写入文本文件。我需要用一些特殊功能来实现这个目标吗?
import java.util.*;
import java.io.*;
class First {
public static ArrayList BestRouteArray(int[] someintlist) { //Sorting method to turn int list into array
ArrayList<Integer> BestRouteArray = new ArrayList<Integer>();
for (int index = 0; index < someintlist.length; index++) {
BestRouteArray.add(someintlist[index]);
}
return BestRouteArray;
}
public static void MostEfficientRoute(ArrayList<Integer> array) {
ArrayList<Integer> hotel = array;
int[] BestRoute = new int[hotel.size()]; //Defining Variables
int[] Stop = new int[hotel.size()];
for (int i = 0; i < hotel.size(); i++)
{
BestRoute[i] = (int) (Math.pow((200 - hotel.get(i)), 2)); //Penalty Calculations
Stop[i] = 0; //Stop, to index values
for (int j = 0; j < i; j++)
{
if (BestRoute[j] + Math.pow((200 - (hotel.get(i) - hotel.get(j))), 2) < BestRoute[i])
{
BestRoute[i] = (int) (BestRoute[j] + Math.pow((200 - (hotel.get(i) - hotel.get(j))), 2));
Stop[i] = j + 1;
}
}
}
// Printing values
ArrayList<Integer> Storage = new ArrayList<>();
int index = Stop.length-1;
while(index>=0)
{
Storage.add((index + 1)); //Loop to get index of Hotel and Penalty Values
index = Stop[index]-1;
}
Collections.reverse(Storage); //Reversing order of Storage
ArrayList<Integer> StoredValues = BestRouteArray(BestRoute); //Taking values from best path int list and storing in array list
if(StoredValues.get(0) != 0){
StoredValues.add(0, 0);
}
ArrayList<Integer> Penalty = new ArrayList<>();
ArrayList<Integer> HotelMarker = new ArrayList<>(); //Initializing Penalty, and hotel maker as arraylist
ArrayList<Integer> StorageHotel = Storage; //Storing Storage values in Storage hotel values to manipulate
for(int i = 0; i < Storage.size(); i++) //Using the storage values as an index to pull Penalty values from
{
Penalty.add(StoredValues.get(Storage.get(i)));
}
for(int i = 0; i < Storage.size(); i++) //Creating new array list to hold storage values -1
{
int value = StorageHotel.get(i);
StorageHotel.set(i, (value - 1));
}
for(int i = 0; i < Storage.size(); i++) //Using the new StorageHotel list as index to pull hotel values from
{
HotelMarker.add(hotel.get(StorageHotel.get(i)));
}
for(int i = 0; i < Storage.size(); i++) { //Printing the Values for both Markers and Penalty
System.out.print("Distance: " + HotelMarker.get(i) + " ");
System.out.println("Total Penalty: " + Penalty.get(i) + " ");
}
}
public static void main(String[] args) throws FileNotFoundException, java.io.IOException {
Scanner dataFile = new Scanner(new File("C:\\Users\\USER\\Documents\\Data.txt"));
ArrayList<Integer> hotels = new ArrayList<>();
hotels.add(0);
while (dataFile.hasNextLine()) {
hotels.add(dataFile.nextInt());
}
dataFile.close();
MostEfficientRoute(hotels);
PrintWriter outputFile = new PrintWriter(new FileWriter("C:\\Users\\USER\\Documents\\OutputData.txt", true));
outputFile.append(MostEfficientRoute(hotels));
outputFile.append("account1 balance: ");
outputFile.close();
}
}
答案 0 :(得分:1)
你的函数MostEfficientRoute没有返回任何值..只是void ..所以实际上你没有在文件中写任何东西
outputFile.append(MostEfficientRoute(hotels));
这个电话没有写任何东西 在你的功能 所以而不是
System.out.print("Distance: " + HotelMarker.get(i) + " ");
System.out.println("Total Penalty: " + Penalty.get(i) + " ");
在函数级别声明一个字符串,将上面两行附加到该字符串然后返回该字符串..然后它应该写入文件
像
String response = "";
for(int i = 0; i < Storage.size(); i++) { //Printing the Values for both Markers and Penalty
response += "Distance: " + HotelMarker.get(i) + " ";
response += "Total Penalty: " + Penalty.get(i) + " ";
}
return response ;
还会将返回类型从void
更改为String