scanner.colose()在我使用try/finaly时不起作用


import java.util.Scanner;
public class userInput
{
public static void main(String[]args){
try{
Scanner scanner = new Scanner(System.in);


String name = scanner.nextLine();
int age = scanner.nextInt(); 
scanner.nextLine();
String text = scanner.nextLine();

System.out.println(name + "n" + age + "n" + text);
//scanner.close(); //it works here
} 

finally{
scanner.close(); // does not work here"scanner cannot be resolvedJava(570425394)"
}
}
}

您必须在"try";,所以没关系,现在这是你的代码:

import java.util.Scanner;
public class userInput { 
public static void main(String[]args) {
Scanner scanner = new Scanner(System.in);
try {
String name = scanner.nextLine();
int age = scanner.nextInt(); 
scanner.nextLine();
String text = scanner.nextLine();
System.out.println(name + "n" + age + "n" + text);
} 
finally {
scanner.close();
}
}
}

相关内容

  • 没有找到相关文章

最新更新