简体   繁体   English

使用“try {} catch (Exception e) {}”时扫描仪不接受输入

[英]Scanner not taking input when use “try {} catch (Exception e) {}”

For some reason when I use try and catch with this scanner it instantly jumps to the catch without even taking the input within try {}出于某种原因,当我在此扫描器中使用try和 catch 时,它会立即跳转到catch处,甚至无需在try {}中获取输入

This code is supposed to check if the input happens to be a different type other than int此代码应该检查输入是否恰好是 int 以外的其他类型

I seemed to have forgotten to clarify that the code works when I remove the try and catch当我删除trycatch时,我似乎忘记澄清代码是否有效

int mapsehen;
try {
    mapsehen = scanner.nextInt();
    if (mapsehen == 1) {
        DrawMap();

    }
} catch (Exception e) {
    System.out.println("Something went wrong.");

}
 Scanner scanner = new Scanner(System.in);  // Create a Scanner object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 捕获/尝试在扫描仪的输入不匹配异常中不起作用? - Catch/try not working for input mismatch exception with scanner? 在 try/catch 中进行异常处理的扫描器输入未按预期工作 - Scanner input with exception handling in a try/catch is not working like intended 这个try和catch(Exception e)仅捕获异常 - This try and catch(Exception e) is only catching the exception 即使在Catch中使用NextLine,来自扫描仪的用户输入也会在try / catch块中导致无限循环 - User input from scanner results in an infinite loop in try/catch block even when using NextLine in Catch 使用扫描仪输入初始化 object 并尝试捕获无效输入或不匹配 - java - Use scanner input to initialize an object and also try and catch for invalid input or mismatch - java JAVA + try catch(FileNotFoundException e) 进入 catch(Exception e)? - JAVA + try catch(FileNotFoundException e) going in catch(Exception e)? 为什么在try catch和while collocation时扫描仪不能再次输入数字 - Why can't the scanner input numbers again when try catch and while collocation 当x =负数时如何使用try catch引发异常 - How to use a try catch to throw an exception when x = negative 尝试抓住扫描仪 - try catch for Scanner 使用 try-catch 在用户输入 == null 时捕获异常,但它不起作用 - Using try-catch to catch exception when user input == null but it won't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM