最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】实现Java中控制台中输入字符串

Java crifan 3182浏览 0评论

【背景】

折腾:

【教程】模拟登陆百度之Java代码版

期间,需要在控制台console中获得输入的用户名和密码。

【解决过程】

1.参考:

input String by Console [closed]

去试试:

            String strBaiduUsername = "";
        	String strBaiduPassword = "";
            String strInput = null;
            Scanner inputReader = new Scanner(System.in);
            System.out.println("Please Enter Your Baidu Username:" );
            strInput = inputReader.nextLine();
            System.out.println("You entered Baidu Username is : " + strInput);

然后就可以了:

 

输入了crifan后,回车:

input reader wait your input

然后可以获得对应的输入:

got entered string crifan

再去打印出来看看:

can ouput entered string crifan

 

【总结】

Java中,想要获得控制台中输入的字符串,可以用如下这种的代码:

String strBaiduUsername = "";
String strInput = null;
Scanner inputReader = new Scanner(System.in);
System.out.println("Please Enter Your Baidu Username:" );
strInput = inputReader.nextLine();
System.out.println("You entered Baidu Username is : " + strInput);

即可。

转载请注明:在路上 » 【已解决】实现Java中控制台中输入字符串

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.158 seconds, using 22.10MB memory