否定不使用.equalsIgnoreCase()

时间:2018-04-08 22:55:03

标签: 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")));

任何人都有任何想法,为什么这不能给我我想要的东西?

1 个答案:

答案 0 :(得分:6)

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

在英语中,这意味着只要isRaceHorse不等于"y" isRaceHorse不等于{{1},循环就会继续}。由于它不能同时与"n""y"相同,我很确定您要使用"n"代替:

&&