gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:19:14 25 4
gpt4 key购买 nike

//程序总是打印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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com