-3

我需要一个代码来在控制台中编写字符串并与数组进行比较。

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

然后将 Array 与 co 进行比较...

4

1 回答 1

5

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

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

执行1:

Please enter: hello world

输入:

hello world

输出:

You entered :hello world
input is: hello world

执行2:

Please enter: hello world

输入:

hello

输出:

You entered :hello 
input is not: hello world
于 2012-12-12T10:26:10.670 回答