0
Who can tell me which mistake i made?
This is the codes import java.util.Scanner; public class Program { public static void main(String[ ] args) { String input1 = new Scanner(System.in).nextLine(); String input2 = new Scanner(System.in).nextLine(); if (input1 == piaoyi){ System.out.println("smart");} else if (input2 == xujun){ System.out.println("stupid");} } }
6 Réponses
+ 10
/*you only need to create one object of Class Scanner*/
Scanner sc = new Scanner(System.in);
String str1 = sc.nextLine();
String str2 = sc.nextLine();
if(str1.equals(str2)){/*doSomthing*/}
else{/*doSomthingElseIfNotEqual*/};
System.out.print(str1+"\n"+str2);
+ 2
piaoyi and xujun look like variable names, but they haven't been defined. Did you mean to write them as strings for comparison? wrap them in " " and the code will compile.
Also in Java you should compare strings with .equals() rather than == e.g.
"myString".equals("anotherString")
+ 1
Like Dan Walker said you should use the method equals() to test for the actual value with objects, because when you use == you're only testing if both objects have the same reference.
0
Plz,come one to help me
0
Maybe try this:
if (input1 == "piaoyi"){...}
and
else if (input2 == "xujun"){...}
If it doesn't work, try this:
if (input1.equals("piaoyi")){...}
and
if (input2.equals("xujun")){...}
0
Thanks,guys.