【问题标题】:How to take user input in jshell script?如何在 jshell 脚本中获取用户输入?
【发布时间】:2021-02-28 11:52:54
【问题描述】:

问题:

如何在 jshell 脚本 中获取用户输入?还是我做错了什么?

注意:不是how to pass arguments to 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

【问题讨论】:

  • 两种查看方式。如果我认为int n1 = in.nextInt(); 之后的脚本中的输入是您手动输入的。如果从脚本中删除那些 Return(enter) 并将其作为单行运行,语句用分号分隔,会发生什么情况?但有趣的是,脚本与逐行的比较很高兴看到这里的区别,不要忘记throws NoSuchElementException - if input is exhausted
  • @nullpointer 我正在考虑第一种方式。添加in.hasNextLine()check(用于指向它的tnx)摆脱异常。但现在感觉我需要某种暂停(按住直到用户进入)..??第二种方式......我是一名学生(不是以英语为母语的人),它让我的大脑“很少”看到我刚刚读到的东西。我读了你的帖子几次,想想我刚刚读到的,尝试单行脚本(结果相同),再次阅读你的帖子,再想一想......仍然感到迷茫。我错过了吗?顺便说一句,感谢您的宝贵时间。

标签: java-9 jshell


【解决方案1】:

默认情况下,jshell 将执行委托给远程 VM。如果您通过--execution local,它使用相同的VM 进程,它提供System.in 的实例,如预期的那样。根据您的问题量身定制,以下调用应该可以解决问题:

jshell --execution local hello.java

通过jshell --help-extra查看详细信息或在https://docs.oracle.com/en/java/javase/11/docs/api/jdk.jshell/module-summary.html浏览API文档

【讨论】:

  • 这是我需要的。
  • 似乎带有--execution local,它会忽略--classPath参数。不知道为什么
  • 添加-J 应该可以解决问题。请参阅jshell --help 及其手册以获取帮助,尤其是关于-J-R-C 选项。
【解决方案2】:

您可以解决这个问题,但不能直接使用基于 JShell 的代码。

有这个项目jshell_script_executor:https://github.com/kotari4u/jshell_script_executor

您可以下载它,并在JShellScriptExecutor.java内部进行小修改

来自

try(JShell jshell = JShell.create()){

// This call will map System.in in your main code
// to System.in inside JShell evaluated code
try(JShell jshell =
  JShell.builder()
    .in(System.in)
    .out(System.out)
    .err(System.err)
    .build()){

和(也)对您的代码进行小幅修改(我知道这不是您正在寻找的 - 我们在这里不使用扫描仪):

/* Put this code into file.jshell */
import java.io.*;

InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);
int n1;
System.out.print("Enter the number: ");
n1 = Integer.parseInt(in.readLine());

int n2;
System.out.print("Enter the number: ");
n2 = Integer.parseInt(in.readLine());

System.out.println("n1 + n2 = " + (n1 + n2));

你可以让它运行:

> javac src/main/java/com/sai/jshell/extension/JShellScriptExecutor.java
> java -cp src/main/java com.sai.jshell.extension.JShellScriptExecutor ./file.jshell
Enter the number: 1
Enter the number: 2
n1 + n2 = 3

嗯...实际上它也可以与您的代码一起使用 - 稍作修改:

/* Put this code into file_scanner.java */
import java.util.Scanner;

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));

试一试

> java -cp src/main/java com.sai.jshell.extension.JShellScriptExecutor ./file_scanner.java
Enter number n1: 1
Enter number n2: 2
n1 + n2 = 3

【讨论】:

    【解决方案3】:

    从JDK11可以直接执行java源文件:

    $java Script.java
    

    Launch Single-File Source-Code Programs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-24
      相关资源
      最近更新 更多