use*_*638 1 java loops for-loop dead-code
这段代码一直i++
在for循环中给我一个死代码警告,并且i
由于某种原因它没有递增!
import java.util.Scanner;
public class HideThatNumber {
/**
* @param args
*/
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int enc=input.nextInt();
int tur=0;
String test="";
double x;
for (int i=1;i<10;i++){
test="";
test+=i;
test+=enc;
x=Integer.parseInt(test);
x/=11;
if(x==Math.round(x));{
tur=i;
break;
}
}
if(tur==0)
System.out.println("Impossible");
else
System.out.println(Integer.parseInt(test)/11);
}
}
Run Code Online (Sandbox Code Playgroud)
Roh*_*ain 13
if(x==Math.round(x)); <--semi-colon
{
tur=i;
break;
}
Run Code Online (Sandbox Code Playgroud)
在你的for循环中,你已经在你的结尾放了一个分号if
.因此,下一个block
代码将在任何情况下执行,因此您将在第一次迭代后中断循环.
{
tur=i;
break;
}
Run Code Online (Sandbox Code Playgroud)
无论您的if条件如何评估,都将执行此块.你break
离开了.
因此你得到了警告,因为i++
永远不会被执行.