나는 나의 수업 중 하나를 위해 짧은 프로그램을 썼고, 나는 그걸 알아낼 수없는 뭔가가있다. 그래서 나는 파일에 100 개의 무작위 정수를 쓰고, 데이터를 다시 읽고, 정수를 증가 순서대로 인쇄하기로되어 있습니다. 모든 것이 정상적으로 작동하지만 출력 파일에 내 최종 정렬 목록이 표시되지 않습니다. 누구나 볼 수 있습니까?파일의 입/출력
여기 내 코드입니다 :
private final static int NUMBEROFRANDOM = 100;
public static void main(String[] args) throws IOException {
// Creating my file
java.io.File file = new java.io.File("Question1.txt");
// If it already exists, print a message and terminate program
if (file.exists()) {
System.out.println("File already exists.");
System.exit(0);
}
// Creating my PrintWriter object
PrintWriter output = new PrintWriter(file);
// Creating 100 random numbers between 0 and 100 and printing them on the file
for (int i = 0; i < NUMBEROFRANDOM; i++) {
int number = (int) (Math.random() * 101);
output.print(number + " ");
}
// Creating my Scanner object
Scanner input = new Scanner(file);
// Creating my array list to store the sorted list of 100 elements
ArrayList<Integer> sortedList = new ArrayList<Integer>();
// Reading the elements from the file and adding them into my array list
while (input.hasNext()) {
sortedList.add(input.nextInt());
}
//Sorting elements from array list
Collections.sort(sortedList);
// Printing the elements in increasing order
for (int i = 0; i < sortedList.size(); i++) {
//System.out.println(sortedList.get(i));
output.print(sortedList.get(i));
}
// Closing my objects
input.close();
output.close();
}
가 대단히 감사합니다, 어떤 도움은 매우 감사합니다!
숫자를 만든 후에 출력을 닫아야합니다. – notyou
누군가가 나를 때리는 것을 본다 - 내 컴퓨터가 너무 오랫동안 내 IDE를 열어 보냈다. :) – notyou