问题:
如何在jshell 脚本中获取用户输入?或者我做错了什么?
注意:我不是在寻找如何将参数传递给 jshell script。
例子:
例如脚本hello.java
:
Scanner in = new Scanner(System.in);
System.out.print("Enter number n1: ");
int n1 = in.nextInt();
System.out.print("Enter number n2: ");
int n2 = in.nextInt();
System.out.println("n1 + n2 = "+ (n1 +n2));
/exit
如果我在 jshell 中逐行键入它会起作用,但是我运行jshell hello.java
它不会。抛出java.util.NoSuchElementException
。
我得到的输出:
@myMint ~/Java $ jshell hello.java
Enter number n1: | java.util.NoSuchElementException thrown:
| at Scanner.throwFor (Scanner.java:858)
| at Scanner.next (Scanner.java:1497)
| at Scanner.nextInt (Scanner.java:2161)
| at Scanner.nextInt (Scanner.java:2115)
| at (#3:1)
Enter number n2: | java.util.NoSuchElementException thrown:
| at Scanner.throwFor (Scanner.java:858)
| at Scanner.next (Scanner.java:1497)
| at Scanner.nextInt (Scanner.java:2161)
| at Scanner.nextInt (Scanner.java:2115)
| at (#5:1)
n1 + n2 = 0
我的系统:Linux Mint 18.2(x64),JShell 版本 9.0.1