使用java“if”时遇到问题陈述
我试图通过编写一个简单的每周计划程序来自学java,代码中的if语句让我很伤心,这是我的代码:
import java.util.Scanner;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class Week
{
public static void main(String[] args) throws IOException
{
Scanner inorout = new Scanner(System.in);
System.out.println("Do you want to read or write (r/w)");
String decision = inorout.nextLine();
System.out.println(decision);
if(decision == "r")
{
System.out.println("It has gone into the if");
Scanner namer = new Scanner(System.in);
System.out.println("What is the name of the week you want to read?");
String r = namer.nextLine();
Scanner s = new Scanner(new File(r + ".txt"));
System.out.println("What is Your Name?");
String name = s.nextLine();
System.out.println("Welcome " + name);
}
}
}
当我使用Drjava编译程序并输入“r”时,它不会运行如果声明,我不知道出了什么问题,非常感谢您的帮助
I'm trying to teach myself java by writing a simple weekly planner program and the if statment in my code is giving me grief, here is my code:
import java.util.Scanner;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class Week
{
public static void main(String[] args) throws IOException
{
Scanner inorout = new Scanner(System.in);
System.out.println("Do you want to read or write (r/w)");
String decision = inorout.nextLine();
System.out.println(decision);
if(decision == "r")
{
System.out.println("It has gone into the if");
Scanner namer = new Scanner(System.in);
System.out.println("What is the name of the week you want to read?");
String r = namer.nextLine();
Scanner s = new Scanner(new File(r + ".txt"));
System.out.println("What is Your Name?");
String name = s.nextLine();
System.out.println("Welcome " + name);
}
}
}
When I compile the program using Drjava and input "r" it doesn't run the if statment, and i'm at a loss as to whats wrong, help would be much appriciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
Java 中的字符串通常使用
equals
进行比较,而不是使用==
。Strings in Java are compared usually with
equals
not with==
.字符串决策
不会与文字“r”
具有引用相等性。您需要使用String.equals
方法。The
String decision
will not have reference-equality with the literal"r"
. You need to useString.equals
method.您应该使用
decision.equals("r")
。==
检查对象引用的相等性,而不是对象本身的相等性。You should use
decision.equals("r")
.==
check for equality of the references to the objects, not for equality of the objects themselves.正如其他人所说,您需要使用
if(decision.equals("r"))
而不是if(decision == "r")
。下面是关于 Java 中字符串引用如何工作的简要说明:
String
类保留当前正在使用的字符串池,如果将文字"r"
分配给decision
或decision
使用.intern()
内部化(放入字符串池中),它将从字符串池中获取它,并且参考文献将是相等的。但由于decision
是在运行时使用用户输入设置的,因此它不会自动内化,并且引用不会相等。简而言之,在某些情况下,
decision == "r"
可以计算为true
。但不要依赖它。使用.equals()
代替。As others have said, you need to use
if(decision.equals("r"))
instead ofif(decision == "r")
.Here's a brief explanation on how string references work in Java:
The
String
class keeps a pool of strings that are currently in use, and if a literal"r"
was assigned todecision
, ordecision
was internalized (put into the string pool) using.intern()
, it will fetch it from the string pool and the references will be equal. But becausedecision
is set at runtime using user input, it will not be internalized automatically, and the references will not be equal.In short, in some situations
decision == "r"
can evaluate totrue
. But don't rely on it. Use.equals()
instead.== 检查对象相等性,不字符串相等性
您需要使用 equals() 方法
== checks for object equality, not string equality
you instead need to use the equals() method
只是想指出,虽然许多答案中建议的
decision.equals("r")
效果很好,但同样有效的是:这样做的好处是你没有担心
"r"
是否为null
。另请注意,在这种情况下,您可能希望进行不区分大小写的比较(因此用户可以根据需要键入“R”),例如:Just want to point out that while the
decision.equals("r")
suggested in many answers will work just fine, it is equally valid to say:The benefit of this is that you don't have to worry about
"r"
ever beingnull
. Also note that you may want to do a case-insensitive comparison in this case (so the user can type 'R' if they want), like:尝试使用类似的东西:
Try using something like:
你应该使用equals方法来比较两个字符串,字符串是一个对象,这是一个引用,equals()方法会比较两个字符串的内容,但是==会比较两个字符串的地址,
所以,你应该这样写:
you should use the equals method to compare the two strings,the string is a object,this is a reference,the equals() method will compare the content of two strings,but the == will compare the address of two strings
so,you should write like this:
相等与身份
您检查相同引用 (
==
) 或相等对象 (.equals()
) )。原始数据类型(Int、Float 等)和引用与
==
、复杂数据类型(任何普通或自定义类、String、任何可通过new 手动实例化的对象)进行比较
) 必须与 object1.equals(
object2)
进行比较。例如,字符串可能会返回
==
误报,如在此可运行示例中所示:(复制并粘贴到文件StringComparisonMain.java
中,编译,运行)Equality vs. Identity
You check for identical references (
==
), or equal objects (.equals()
).Primitive data types (Int, Float, etc.) and references are compaired with
==
, complex data types (any normal or self-defined class, String, any object intantiatable by hand vianew
) have to be compaired by object1.equals(
object2)
.Strings for example can return false positives for
==
, as can be seen in this runnable example here: (copy&paste into FileStringComparisonMain.java
, compile, run)