java - 否定不适用于 .equalsIgnoreCase()

标签 java

我正在尝试运行一个 do...while 条件不满足,但出于某种原因,无论条件值如何,我的代码每次都会循环。我已经检查了我的调试器,变量的值是正确的,并且根据类似的逻辑满足循环内的条件。出于某种原因,! 不起作用。这是我的代码

do
  {
     if(isRaceHorse.equalsIgnoreCase("Y"))
     {
        System.out.print("Please enter the number of races your horse has been in >> ");
        numRaces = input.nextInt();
        input.nextLine();
        System.out.println("Your horse is named " + name + " it is a " + color + " horse born in " + birthYear + " and it has been in " + numRaces + " races.");
     }
     else if(isRaceHorse.equalsIgnoreCase("N"))
     {
        System.out.println("Your horse is named " + name + " it is a " + color + " horse born in " + birthYear + ".");
     }
     else
     {
        System.out.println("Please use Y for yes and N for no.");
        System.out.print("Is your horse a race horse? Enter Y for yes and N for no >> ");
        isRaceHorse = input.nextLine();
     }
  }while(!(isRaceHorse.equalsIgnoreCase("y")) || !(isRaceHorse.equalsIgnoreCase("n")));

有人知道为什么这没有给我我想要的东西吗?

最佳答案

!(isRaceHorse.equalsIgnoreCase("y")) || !(isRaceHorse.equalsIgnoreCase("n"))

在英语中,这意味着只要 isRaceHorse 不等于 "y" isRaceHorse<,循环就会继续 不等于 "n"。因为它不能同时等于 "y""n",我很确定你想使用 &&相反:

!isRaceHorse.equalsIgnoreCase("y") && !isRaceHorse.equalsIgnoreCase("n")

关于java - 否定不适用于 .equalsIgnoreCase(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49723553/

相关文章:

java - 从 Assets 文件夹中重新缩放图像以适合屏幕

java - 无法运行 Oracle 提供的 Doclet 类

java - Android ScrollView 将不再滚动

java - 如何仅更改一个组件的工具提示颜色?

java - 从 Java 中调用 `scala.Function1[_root_.scala.Predef.String, scala.Any]`

java - jdbc Postgres 驱动程序中的 `ssl` 和 `useSSL` 有什么区别?

java - Spring注入(inject)接口(interface)实现

java - Spring @DateTimeFormat 在将 @PathVariable 转换为 Date 时正在更改时区

java - 更改字符串中的多个字符

java - 验证使用较新的 JDK 构建的库的 API 级别的向后兼容性