简体   繁体   中英

For loop inside of for loop not executing

The second for loop is only executing for the first number(999) in the first for loop. I am trying to have the second for loop execute for each number 999-101 in the second.

int num1 = 999;
int num2 = 999;
int test1 = 0;
String test = "";

for(; num1!=110; num1--){
    System.out.println("--------"+num1);
    for(; num2!=100; num2--){
        System.out.println("++++++++"+num2);
        test1 = num1*num2; 
        test = String.valueOf(test1);
        //System.out.println("checkpoint 2"+"--------------"+test1);
        if(test.length()==5){
            if(test.charAt(0)==test.charAt(5) && test.charAt(1)==test.charAt(4) && test.charAt(2)==test.charAt(3)){
                System.out.println("checkpoint 1"+"----------"+test);
                break;
            }
            else{
                //System.out.println("Fail 1");
            }
        }
    }
}

After the first run of the second loop num2 has the value 100 so the statement num2!=100 is false and it does not execute. Set the value for num2 to get it to execute for each loop:

for(num2=999; num2!=100; num2--) {
  ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM