Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java扫描程序代码If then else_Java_If Statement - Fatal编程技术网

Java扫描程序代码If then else

Java扫描程序代码If then else,java,if-statement,Java,If Statement,我的if-then-else语句总是输出else结果 import java.util.Scanner; public class NiallScanner { public static void main(String[] args) { System.out.println("Hello, What is your name?"); Scanner scanner = new Scanner(System.in); String yourName = sca

我的
if-then-else
语句总是输出
else
结果

import java.util.Scanner;


public class NiallScanner {

public static void main(String[] args) 
{
    System.out.println("Hello, What is your name?");
    Scanner scanner = new Scanner(System.in);
    String yourName = scanner.nextLine();
    System.out.println("Is your name: "+yourName + "?");

    Scanner scanner1 = new Scanner(System.in);
    String isCorrect = scanner1.nextLine();

    if (isCorrect ==  "Yes")
    {
        System.out.println("Thank you for your confirmation!");
    }
    else
    {
        System.out.println("Retry please.");

    }

}
知道为什么吗?顺便说一句,我对java非常陌生,所以我可能忽略了基本的编码错误。

使用
“Yes”。equals(isCorrect)
==
比较对象引用,而不是内容。两个不同的
字符串可以具有相同的内容

或者,您可以使用从字符串池中获取唯一引用;使用
=
运算符可以安全地比较这些值:

"Yes" == isCorrect.intern();
虽然两种方法都有效,但我建议您使用第一种方法。使用
equals
比较对象,使用
=
比较基本体

检查。

使用
equals()
方法,因为
=
比较对象引用它包含的位,以查看两个对象是否引用同一对象。但是
equals()
方法会比较值。因此,在这种情况下,您应该执行:
“Yes”。等于(isCorrect)

如果要检查两个对象是否引用同一个对象,例如:

Object1 x = new Object1();
Object2 y = x;

if(x == y) {
//This will return true because 'y' is refering to object 'x' so both has the bit to  access the object on memory.
}
String hola1 = "hola";
String hola2 = "hola";
if(hola1.equals(hola2)){
 //Return true because both has the same value.
}
但是,如果要按值进行检查,例如:

Object1 x = new Object1();
Object2 y = x;

if(x == y) {
//This will return true because 'y' is refering to object 'x' so both has the bit to  access the object on memory.
}
String hola1 = "hola";
String hola2 = "hola";
if(hola1.equals(hola2)){
 //Return true because both has the same value.
}

使用equals方法比较字符串。==将比较引用而不是内容。请查找更正的程序

public class NiallScanner {

public static void main(String[] args) {
    System.out.println("Hello, What is your name?");
    Scanner scanner = new Scanner(System.in);
    String yourName = scanner.nextLine();
    System.out.println("Is your name: "+yourName + "?");

    Scanner scanner1 = new Scanner(System.in);
    String isCorrect = scanner1.nextLine();


    if (isCorrect.equals("Yes"))
    {
        System.out.println("Thank you for your confirmation!");
    }
    else
    {

        System.out.println("Retry please.");

    }
}

}
阅读