我知道尽可能少的代码重复是更好的做法,所以我决定在整个班级中只声明一个扫描仪,但是我应该在哪里关闭扫描仪对象或者不一定要关闭它,关闭扫描仪有什么作用.
private Scanner scanner;
/**
* Constructor for objects of class Scanner
*/
public Ssss()
{
// initialise instance variables
scanner = new Scanner(System.in);
}
public void enterYourName()
{
System.out.println("Enter your Name");
String name = scanner.nextLine();
System.out.println("Your name is:" + name);
}
public void enterYourAge()
{
System.out.println("Enter your Age");
int age = scanner.nextInt();
System.out.println("Your age is: " + age);
}