如果使用Java语句,则无法获得其他语句 [英] Can't get else if statement to work in Java

查看:109
本文介绍了如果使用Java语句,则无法获得其他语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我正在努力做出这个简单的事情,但它不起作用。我是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. 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");
         }
     } 
 }


推荐答案

嗯,首先,==是该语言的基本操作符。表达式的结果类型是布尔值。为了比较布尔类型,它比较了相同真值的操作数。为了比较引用类型,它比较相同参考值的操作数(即,引用相同的对象或都是null)。对于数字类型,它会比较相同整数值或等效浮点值的操作数。参见 Java语言规范

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 (i.e., refer to the same object or are both null). For numeric types, it compares the operands for the same integer value or equivalent floating point values. See the Java Language Specification.

相比之下,equals()是一个实例方法,它基本上由java.lang.Object类定义。按照惯例,该方法指示接收方对象是否等于传入的对象。 Object类中此方法的基本实现检查引用相等性。其他类(包括您编写的类)可能会覆盖此方法以执行更专业的等效性测试。参见 Java语言规范

In contrast, equals() is an instance method which is fundamentally defined by the java.lang.Object class. 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. Other classes, including those you write, may override this method to perform more specialized equivalence testing. See the Java Language Specification.

大多数人的典型问题是使用==来比较两个字符串,当他们确实应该使用String类的equals()方法。从上面可以看出,当两个引用引用同一个实际对象时,运算符只返回true。但是,对于字符串,大多数用户想要知道两个字符串的值是否相同 - 因为两个不同的String对象可能都具有相同(或不同)的值。

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. From above, you know that the operator will only return "true" when both of the references refer to the same actual object. 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.

     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");
     }

这篇关于如果使用Java语句,则无法获得其他语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆