java 通过控制台输入字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13837065/
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-10-31 14:08:09  来源:igfitidea点击:

input String by Console

java

提问by sarot

I need a code to write String in the console and making compare with Array.

我需要一个代码来在控制台中写入 String 并与 Array 进行比较。

String Array = {"skill"};
Console co=System.console();

then compare Array with co...

然后将 Array 与 co...

回答by codeMan

here is an example to read a string from the keyboard and compare the string with another string

这是从键盘读取字符串并将该字符串与另一个字符串进行比较的示例

import java.util.Scanner;
public class Main
{
    public static void main(String args[])
    {
        String input = null;
        String compareString = "hello world";
        Scanner inputReader = new Scanner(System.in);
        System.out.println("please enter: hello world" );

        // here is how you take the input from keyboard

        input = inputReader.nextLine();
                // this is how you write to console
        System.out.println("You entered :" + input);

        //Here is how you compare two string
        if(compareString.equals(input))
        {
            System.out.println("input is: hello world");
        }
        else
        {
            System.out.println("input is not: hello world");
        }
    }
}      

Execution1:

执行1:

Please enter: hello world

Input:

输入:

hello world

output:

输出:

You entered :hello world
input is: hello world

Execution2:

执行2:

Please enter: hello world

Input:

输入:

hello

output:

输出:

You entered :hello 
input is not: hello world