0
我正在爲uni編寫一個實驗任務的程序,代碼將會非常明顯地發生什麼,但是當它詢問第一行時,即循環中的第一個1 ,並插入一個字符串並回車,它會自動跳轉到循環中的最後一個增量(5)任何想法?for循環不遍歷所有增量
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
public class limmerickWriter {
public static void main(String[] args)throws Exception {
Scanner limScan = new Scanner(System.in); //scanner to read user input
System.out.println("please enter the name of the file");
String fileName;
fileName = limScan.next(); //filename for the text file
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fileName))); //declaring a new file writer
for (int i = 1; i <= 5; i++) //loop to get 5 seperate lines from the user
{
System.out.println("please enter line " + i);
out.println(limScan.next()); //writes the contents of the scanner to the file
}
out.flush();
out.close();
}
}
您是否嘗試過調試它?代碼對我來說看起來很好。 – Paranaix
我正在使用Eclipse IDE,因此它在編譯前自動調試 – srheayns
不,它不。 「調試」意味着在調試器中運行代碼,或者將調試器附加到應用程序中。 –