java 使用控制台为 Intellij 读取 System.in

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26092784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:16:25  来源:igfitidea点击:

Reading System.in for Intellij using Console

javaintellij-idea

提问by daidaidai

I need to open up the console and type in inputs for my an assignment using Intellij. Eclipse has a way of doing this using the Scannerclass and reading System.inbut running the same code in IntelliJ does not work as I can't type anything into the console.

我需要打开控制台并使用 Intellij 为我的作业输入输入。Eclipse 有一种使用Scanner类和读取的方法来执行此操作,System.in但在 IntelliJ 中运行相同的代码不起作用,因为我无法在控制台中输入任何内容。

Is there any way to do this?
My code is as follows:

有没有办法做到这一点?
我的代码如下:

    public class BasicAssertions {
       @Test
       public void testAssertions(){
           System.out.println("Enter: ");
           Scanner reader = new Scanner(System.in);
           int first = reader.nextInt();
           int second = reader.nextInt();
           String s = reader.next();
           String s2 = reader.next();
           assertTrue(first<=second);
           assertFalse(first+second >100);
           assertNotEquals(s,s2);
           assertNotNull(s2);    
       }    
   }

回答by daidaidai

Resolved. A public static void main()method is required for the correct console to appear, otherwise running using the default JUnit Test configurationwill only result in a console that doesn't receive inputs.

解决。public static void main()需要一个方法才能显示正确的控制台,否则使用默认的JUnit 测试配置运行只会导致控制台不接收输入。

回答by lc2817

Just click on the console window and type, it works for me on IntelliJ 13 CE. See the image below, I clicked in the console and wrote the text (it appears in green then, I typed enter and it shows up):

只需单击控制台窗口并键入,它在 IntelliJ 13 CE 上对我有用。参见下图,我在控制台中单击并编写了文本(然后它显示为绿色,我输入 enter 并显示):

enter image description here

enter image description here