Weekend teacher Let write a guess the number of Java games, randomly generate a different four-digit number, and then according to the user input to judge, give the corresponding results
Each time you enter a 4-digit number, according to the standard,
is divided into digital existence (recorded as B);
The number exists and is in the correct position (recorded as a)
Example: Generate a number 1234
Guessing number: 1367 (hint Quiz conclusion: 1A1B)
And then I have the result. The return type of the definition is a string, which is then received with a string Jieguo, which should be compared to the string "4 a" when it is finally judged whether to guess the number.
At first I used the IF (jieguo== "4 A") to determine whether two strings were the same, but found no.
So find the mother found the difference:
When comparing strings, if you comparetwo objects with "= =", the memory addresses of two objects are compared, so they are not equal. Even though their contents are equal, the memory addresses of different objects are not the same.
So there is the Equals ();
For example:
String A=new string ("AAA");
String B=new string ("AAA");
The two strings should of course be equal.
If a==b with an expression, the value of the expression is false
If A.equals (B) is used with an expression, the value of the expression is true;
Equals () compares the contents of the object (case-sensitive letters).
Determining whether strings are equal in Java