if语句无法在Java中工作 [英] Can't get else if statement to work in Java

查看:50
本文介绍了if语句无法在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).对于数字类型,它将比较相同整数值或等效浮点值的操作数.请参见

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类中此方法的基本实现检查引用是否相等.其他类,包括您编写的类,可能会覆盖此方法以执行更专门的等效测试.请参见

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

这篇关于if语句无法在Java中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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