java - Java初学者,input.close(); 在做while循环之后,如何?
问题描述
我完全是初学者,无法弄清楚这里的问题 - 我希望在用户输入“退出”后关闭扫描仪输入。当我尝试我的代码时,它显示“退出>”,我仍然可以输入。是否与 input.close() 的范围有关?
String command;
do {
System.out.print("> ");
command = input.nextLine();
switch (command) {
case "register new dog":
System.out.println("You gave command: register new dog");
System.out.println("Command? ");
break;
case "increase age":
System.out.println("You gave command: increase age");
System.out.println("Command? ");
break;
case "list dogs":
System.out.println("You gave command: list dogs");
System.out.println("Command? ");
break;
case "remove dogs":
System.out.println("You gave command: remove dogs");
System.out.println("Command? ");
break;
case "exit":
System.out.println("Goodbye!");
break;
default:
System.out.println("Error: invalid command, choose again: ");
System.out.println("Register new dog");
System.out.println("Increase age");
System.out.println("List dogs");
System.out.println("Remove dogs");
System.out.println("Exit");
}
} while (command != "exit");
input.close();
解决方案
推荐阅读
- python - 必须具有相同的数据类型,得到 tf.uint8 != tf.float32(行为克隆模拟器)
- reactjs - ReactJS如何仅在条件为真时获取?
- android - 在 Android Studio 中,ListView 中的按钮不起作用
- rust - 为什么我收到错误:捕获的变量无法逃脱 `FnMut` 闭包体?
- discord - 我怎样才能使不和谐的机器人耳聋?
- r - 索引匹配双列,如 Excel,但在 R 中
- php - 如何使用 post 方法传递变量?
- mongodb - 当我尝试运行 mongo 命令时,我得到以下错误
- javascript - javascript 错误:无法读取未定义的属性“scrollIntoView”
- javascript - 通过休息 POST 表单数据