简体   繁体   English

使用Console读取System.in for Intellij

[英]Reading System.in for Intellij using Console

I need to open up the console and type in inputs for my an assignment using Intellij. 我需要打开控制台并使用Intellij输入我的作业输入。 Eclipse has a way of doing this using the Scanner class and reading System.in but running the same code in IntelliJ does not work as I can't type anything into the console. 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);    
       }    
   }

Resolved. 解决。 A public static void main() method is required for the correct console to appear, otherwise running using the default JUnit Test configuration will only result in a console that doesn't receive inputs. 要显示正确的控制台,需要使用public static void main()方法,否则使用默认的JUnit Test配置运行只会导致控制台无法接收输入。

Just click on the console window and type, it works for me on IntelliJ 13 CE. 只需单击控制台窗口并键入,它就适用于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): 看到下面的图片,我点击了控制台并写了文字(它显示为绿色然后,我输入并显示):

在此输入图像描述

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM