我有一个关于如何使用scanner对象在java中比较字符串的问题? 。我要比较的东西分别是4个州和首都。
这是我到目前为止所尝试的:
String myString1 = new String ("state");
String myString2 = new String ("capitalstate");
System.out.println ("input state");
myString1=input.next();
if (myString1.equals("malaysia"))
{
myString2 = "kuala lumpur";
System.out.println ("capital state for " +myString1+ " is " +myString2);
}
else if (myString1.equals("south korean"))
{
myString2 = "seoul";
System.out.println ("capital state for " +myString1+ " is " +myString2);
}
答案 0 :(得分:-1)
您的代码可以是以下内容,应该可以正常运行:
Scanner input = new Scanner(System.in);
String myString1 = new String ("state");
String myString2 = new String ("capitalstate");
System.out.println ("input state");
myString1=input.next();
if (myString1.contains("malaysia"))
{
myString2 = "kuala lumpur";
System.out.println ("capital state for " +myString1+ " is " +myString2);
}
else if (myString1.equals("south korean"))
{
myString2 = "seoul";
System.out.println ("capital state for " +myString1+ " is " +myString2);
}