Java中的If-else语句
我的程序有一个小问题,我确信它存在于这个循环和if-else语句中:Java中的If-else语句,java,if-statement,for-loop,break,Java,If Statement,For Loop,Break,我的程序有一个小问题,我确信它存在于这个循环和if-else语句中: for (int i = 0; i < xArray.length; i++) { if(searchNumber == xArray[i]) { found = true; if(found) { System.out.
for (int i = 0; i < xArray.length; i++)
{
if(searchNumber == xArray[i])
{
found = true;
if(found)
{
System.out.println("We have found your number " + searchNumber + " at position " + i + " in the array. " );
}
else
{
System.out.println("No match was found.");
}
break;
}
}
for(int i=0;i
当我执行该程序时,如果找到searchNumber(通过带有键盘输入的扫描仪获取),则它将显示相应的消息,表示已找到,但如果未找到,则不会显示任何消息。我试着通过讲述每一行代码都在做什么来调试它,但当我进入这个循环时,我感到非常困惑 将变量
found
设置为true,然后进行测试if(found)
,该测试也将始终为true。else
子句永远不会执行,因为found
永远不会为false
根据您的描述,听起来您需要在循环外部将
found
初始化为false。然后,如果您得到匹配,则将其设置为true。然后可以将if/else语句移到for循环之外,并按预期的方式运行。将变量found
设置为true,然后进行测试if(found)
,该测试也将始终为true。else
子句永远不会执行,因为found
永远不会为false
根据您的描述,听起来您需要在循环外部将
found
初始化为false。然后,如果您得到匹配,则将其设置为true。然后可以将if/else语句移到for循环之外,并按预期的方式运行。将变量found
设置为true,然后进行测试if(found)
,该测试也将始终为true。else
子句永远不会执行,因为found
永远不会为false
根据您的描述,听起来您需要在循环外部将
found
初始化为false。然后,如果您得到匹配,则将其设置为true。然后可以将if/else语句移到for循环之外,并按预期的方式运行。将变量found
设置为true,然后进行测试if(found)
,该测试也将始终为true。else
子句永远不会执行,因为found
永远不会为false
根据您的描述,听起来您需要在循环外部将
found
初始化为false。然后,如果您得到匹配,则将其设置为true。然后可以将if/else语句移到for循环之外,并按照您期望的方式运行。确保found
以false开始。然后在循环完成(或中断)后进行打印
boolean-found=false;
int i//谢谢@DaveNewton!
对于(i=0;i
确保找到的以false开始。然后在循环完成(或中断)后进行打印
boolean-found=false;
int i//谢谢@DaveNewton!
对于(i=0;i
确保找到的以false开始。然后在循环完成(或中断)后进行打印
boolean-found=false;
int i//谢谢@DaveNewton!
对于(i=0;i
确保找到的以false开始。然后在循环完成(或中断)后进行打印
boolean-found=false;
int i//谢谢@DaveNewton!
对于(i=0;i
对于初学者来说,您过早地进行了优化。除非阵列非常大,否则不需要中断检查。高效是好的,但不能以不工作为代价
当您希望提前结束循环时,我喜欢使用while循环而不是for循环的这种方法:
//init our sentinel flag and our counter
boolean found = false;
int i = 0 ;
//while we have not found anything and we are less than the size of the array
while (!found && i < xArray.length)
{
//check our condition
found = (searchNumber == xArray[i]);
i++;
}
//finally react to the terminal state, found or not found and output as needed
if(found)
{
System.out.println("We have found your number " + searchNumber + " at position " + i + " in the array. " );
}
else
{
System.out.println("No match was found.");
}
//初始化我们的哨兵标志和计数器
布尔值=false;
int i=0;
//虽然我们没有找到任何东西,但我们的大小小于数组的大小
而(!found&&i
这在功能上等同于f
//init our sentinel flag and our counter
boolean found = false;
int i = 0 ;
//while we have not found anything and we are less than the size of the array
while (!found && i < xArray.length)
{
//check our condition
found = (searchNumber == xArray[i]);
i++;
}
//finally react to the terminal state, found or not found and output as needed
if(found)
{
System.out.println("We have found your number " + searchNumber + " at position " + i + " in the array. " );
}
else
{
System.out.println("No match was found.");
}
int position = xArray.indexOf(searchNumber);
if (position > -1) {
System.out.println("We have found your number " + searchNumber + " at position " + position + " in the array. " );
} else {
System.out.println("No match was found.");
}
java.util.Arrays.asList(xArray).indexOf(searchNumber)
for(int i : xArray){ //Foreach loop to simplify
if(i == searchNumber){//True if i == number we're searching for
System.out.println("We have found your number " + searchNumber + " at position " + i + " in the array. " );//Print the message
found = true; //The number has been found
break; //cancel the loop as the number is found.
}
}
//The array has been searched completely
if(!found){//if the number has not been found
System.out.println("No match was found.");//Print the message
}