简体   繁体   English

如果使用Java语句,则无法获得其他语句

[英]Can't get else if statement to work in Java

Ok I am trying to make this simple thing but it won't work. 好吧,我正在努力做出这个简单的事情,但它不会起作用。 I am a beginner in Java and would like some help. 我是Java的初学者,想要一些帮助。 Every time I run the code below I get the output That is not a valid option . 每次我运行下面的代码,我得到输出, 这不是一个有效的选项 What am I doing wrong? 我究竟做错了什么?

 package test;

 import java.util.Scanner;

 public class options {
     public void options() {
         Scanner scnr = new Scanner(System.in);
         String slctn;

         System.out.println("What would you like to do?");
         System.out.println("a) Travel the expedition");
         System.out.println("b) Learn more about the expedition");

         slctn = scnr.nextLine();
         if (slctn == "a"){
             travel exeTravel = new travel();
             exeTravel.travel();
         }else if (slctn=="b"){
             learn exeLearn = new learn();
             exeLearn.learn();
         }else{
             System.out.println("That is not a valid option");
         }
     } 
 }

Well, first off, == is a fundamental operator in the language. 好吧,首先,==是该语言的基本操作符。 The result type of the expression is a boolean. 表达式的结果类型是布尔值。 For comparing boolean types, it compares the operands for the same truth value. 为了比较布尔类型,它比较了相同真值的操作数。 For comparing reference types, it compares the operands for the same reference value (ie, refer to the same object or are both null). 为了比较引用类型,它比较相同参考值的操作数(即,引用相同的对象或都是null)。 For numeric types, it compares the operands for the same integer value or equivalent floating point values. 对于数字类型,它会比较相同整数值或等效浮点值的操作数。 See the Java Language Specification . 请参阅Java语言规范

In contrast, equals() is an instance method which is fundamentally defined by the java.lang.Object class. 相反,equals()是一个实例方法,它基本上由java.lang.Object类定义。 This method, by convention, indicates whether the receiver object is "equal to" the passed in object. 按照惯例,该方法指示接收方对象是否“等于”传入的对象。 The base implementation of this method in the Object class checks for reference equality. Object类中此方法的基本实现检查引用相等性。 Other classes, including those you write, may override this method to perform more specialized equivalence testing. 其他类(包括您编写的类)可能会覆盖此方法以执行更专业的等效性测试。 See the Java Language Specification . 请参阅Java语言规范

The typical "gotcha" for most people is in using == to compare two strings when they really should be using the String class's equals() method. 对于大多数人来说,典型的“陷阱”是使用==来比较两个字符串,当它们确实应该使用String类的equals()方法时。 From above, you know that the operator will only return "true" when both of the references refer to the same actual object. 从上面可以看出,当两个引用引用同一个实际对象时,运算符只返回“true”。 But, with strings, most uses want to know whether or not the value of the two strings are the same -- since two different String objects may both have the same (or different) values. 但是,对于字符串,大多数用户想知道两个字符串的值是否相同 - 因为两个不同的String对象可能都具有相同(或不同)的值。

     slctn = scnr.nextLine();
     if (slctn.equals("a")){
         travel exeTravel = new travel();
         exeTravel.travel();
     }else if (slctn.equals("b")){
         learn exeLearn = new learn();
         exeLearn.learn();
     }else{
         System.out.println("That is not a valid option");
     }

slctn.equals("a") will work. slctn.equals("a")将起作用。

Read this to understand why: What is difference between == and equals() in java? 阅读本文以了解原因: java中==和equals()之间的区别是什么?

In Java, when you need to compare two objects for equality (that is, to determine if they have the same value) you must use equals() . 在Java中,当您需要比较两个对象是否相等 (即,确定它们是否具有相同的值)时,必须使用equals() The == operator is used for testing if two objects are identical , that is: if they're exactly the same object in memory. ==运算符用于测试两个对象是否相同 ,即:如果它们与内存中的对象完全相同。 In your code, replace this: 在您的代码中,替换为:

slctn == "a"
slctn == "b"

With this: 有了这个:

"a".equals(slctn)
"b".equals(slctn)

Also notice that it's a good idea to invert the order of the comparison ( "a" before slctn ), just in case slctn is null. 另请注意,反转比较顺序( slctn之前的"a" )是个好主意,以防slctn为空。

In java when matching any object the == operator will only match the reference of those two objects. 在java中匹配任何对象时,==运算符只匹配这两个对象的引用。

If we take your example slctn == "a". 如果我们以你的例子slctn ==“a”为例。 Say slctn has its reference value at abc123, your other sting "a" will have a different reference value as it is not the same object. 假设slctn的参考值在abc123,你的另一个sting“a”会有不同的参考值,因为它不是同一个对象。

The method .equals checks what the letters in the string object are and matches the value of the letters in the two strings. 方法.equals检查字符串对象中的字母是什么,并匹配两个字符串中字母的值。 Therefore if your object slctn contains "a", it will match with the string "a" 因此,如果您的对象slctn包含“a”,它将与字符串“a”匹配

In java == operator compare reference of two objects, for sample : 在java ==运算符比较两个对象的引用中,对于示例:

String s_1 = new String("Sample");
String s_2 = new String("Sample");

System.out.println(s_1 == s_2);

result will is : 结果将是:

false

this happen because s_1 is a reference at memory and s_2 is difference refernce at memroy also. 发生这种情况是因为s_1是记忆中的参考,而s_2也是记忆中的差异参考。 For solve this issue , you have to compare tow objects by equals method. 要解决此问题,必须使用equals方法比较两个对象。 for sample 样品

String s_1 = new String("Sample");
String s_2 = new String("Sample");

System.out.println(s_1.equals(s_2));

result will is : 结果将是:

true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM