加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 16:37:03 所属栏目:Java 来源:网络整理
导读:我需要打开控制台,并使用Intellij输入我的作业输入. Eclipse有一种方法可以使用Scanner类并读取System.in,但在IntelliJ中运行相同的代码不起作用,因为我无法在控制台中输入任何内容. 有没有办法做到这一点? 我的代码如下: public class BasicAssertions {
我需要打开控制台,并使用Intellij输入我的作业输入.
Eclipse有一种方法可以使用Scanner类并读取System.in,但在IntelliJ中运行相同的代码不起作用,因为我无法在控制台中输入任何内容.

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

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

解决方法

解决.要使正确的控制台出现,需要使用public static void main()方法,否则使用默认的JUnit Test配置运行只会导致不接收输入的控制台.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读