我试图运行一个do ...虽然条件不满足但由于某种原因我的代码每次都会循环,无论条件的值如何.我已经检查了我的调试器,并且变量的值是正确的,并且根据类似的逻辑满足了循环内的条件.由于某种原因,这!是行不通的.这是我的代码
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")));
Run Code Online (Sandbox Code Playgroud)
任何人都有任何想法,为什么这不给我我想要的东西?
!(isRaceHorse.equalsIgnoreCase("y")) || !(isRaceHorse.equalsIgnoreCase("n"))
Run Code Online (Sandbox Code Playgroud)
在英语中,这意味着只要isRaceHorse不等于"y" 或 isRaceHorse不等于循环,循环就会继续"n".因为它不能等于两者"y"并"n"在同一时间,我敢肯定你想使用&&,而不是:
!isRaceHorse.equalsIgnoreCase("y") && !isRaceHorse.equalsIgnoreCase("n")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
101 次 |
| 最近记录: |