java - if 语句没有采用我的字符串值并将它们评估为 boolean 值(java初学者问题)

标签 java string if-statement boolean

//程序总是打印if语句中写入的行。 //此外,每次我尝试添加 else 语句时 eclipse 都会返回错误

import java.util.Scanner;
import java.util.Arrays;
public class Practice {

public static void main(String[] args)


Scanner input = new Scanner(System.in);
System.out.println("Enter the number of artists you would like to search: ");
int number = input.nextInt();
String junk = input.nextLine();
String []artist = new String[number];

for(int i=0; i < number ; i++)
{
    artist[i]= input.nextLine();
}
System.out.println("Here is the list of artists you searched for: " + Arrays.toString(artist) + ". Is this correct?");

String check = input.nextLine();
if((check.equalsIgnoreCase("yes") || check.equalsIgnoreCase("y")) == false); //this continually returns both print statements in and out of the if statement even if I input something other than yes or y and I have no idea why
{
    System.out.println("Cool! Enjoy your search!"); //this always prints no matter what
} 

System.out.println("Please try again! Sorry for the inconvenience!"); //won't let me add an else statement

最佳答案

条件末尾有一个悬空的 ;

if((check.equalsIgnoreCase("yes") || check.equalsIgnoreCase("y")) == false) // ; was here
{

}

删除它。这称为空语句。您可以将其重写为

if((check.equalsIgnoreCase("yes") || check.equalsIgnoreCase("y")) == false)
    ; 

{
    System.out.println("Cool! Enjoy your search!"); //this always prints no matter what
} 

由于 block {/* ... */} 是有效代码,因此无论如何它都会被执行。

关于java - if 语句没有采用我的字符串值并将它们评估为 boolean 值(java初学者问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19942612/

相关文章:

javascript - css api 的 jquery if else 条件

java - 使用 JQuery ajax 发布表单数据时出现无效参数错误

c++ - 在涉及 AND 的 if 条件下避免流浪指针

java - 在 Jersey 中,如何处理深度嵌套的复杂对象的 @POST 参数?

C++ 在函数中拆分字符串类不使用 strtok()

java - 保存数据+字符串

c++ - rdbuf() 奇怪的行为

javascript - 在函数结束时检查执行错误的代码?

java - 如何在 Maven 中构建项目时执行已编译的 jar

java - 从 Java 执行 scanimage 命令