Java determines whether strings are the same

System.out.print("请输入前进方向:");
String direction = scan.nextLine();
 if(direction == "west")
    {
	a = 1;
    }

应改为:
if(direction.equals("west"))
	  {
		  a=1;
	  }

It was found that there was no way to compare, and I later felt that it was impossible to do so, as expected. . .
If two strings have the same character and length, return true if the equals () method is used. At the same time, the equals () method is case sensitive when comparing.

String a1;
String a2;
a1.equals(a2);
相同返回true,否则false
Published 208 original articles · Like 156 · Visits 140,000+

Guess you like

Origin blog.csdn.net/z2431435/article/details/105465984