如何循环一个try/catch语句?我正在编写一个程序,使用Scanner从键盘读取文件。所以如果文件不存在,我希望程序会显示“文件不存在,请重试。”然后让用户键入不同的文件名。我尝试了一些不同的方法来实现这个目标,但是,所有的尝试都导致程序崩溃。
以下是我拥有的内容:
try {
System.out.println("Please enter the name of the file: ");
Scanner in = new Scanner(System.in);
File file = new File(in.next());
Scanner scan = new Scanner(file);
} catch (Exception e) {
e.printStackTrace();
System.out.println("File does not exist please try again. ");
}
for
或while
(或其他 - 在此之前请勿纠正)来循环。 - Matt Ball