maven if else语句返回错误答案[重复]

q1qsirdb  于 2024-01-06  发布在  Maven
关注(0)|答案(1)|浏览(205)

此问题在此处已有答案

How do I compare strings in Java?(23个回答)
上个月关闭。

  1. import java.util.Scanner;
  2. public class orOperator {
  3. public static void main(String args[]) {
  4. System.out.println(" This code is to check you are eligible or not");
  5. Scanner buddha2 = new Scanner(System.in);
  6. System.out.println("Please enter your nationality");
  7. String nationality = buddha2.nextLine();
  8. System.out.println("you are " + nationality);
  9. if (nationality == "indian") {
  10. System.out.println("you are eligible to join the indian army");
  11. } else {
  12. System.out.println("Your are not eligible to join the army");
  13. }
  14. }
  15. }

/else部分返回的是答案而不是if部分/座右铭是将国籍作为输入,然后将能够将结果作为国籍输出/

vpfxa7rd

vpfxa7rd1#

尝试

  1. if (nationality.equals("indian"))

字符串
当使用“==”时,它检查两个变量的引用相等性,这意味着它们是否引用相同的对象。

相关问题