dvdxthl17
dvdxthl17

Reputation: 25

While Loop is running indefinetly and not giving the right Output

I'm just starting out in Java 8 so please excuse my horrendous code. After a few hours of trying this assignment, I'm still not getting any actual results.

The given problem is:

ABC + DBE = EFF

minus minus minus

DEG + FFA = HFI

equal equal equal

CBI + FED = DEJ

I need to find all the corresponding digits and solve all equations by simply trying out numbers. I can only use While-Loops for this assignment.

Here's the code I came up with:

public static void main(String args[]) {
    
    int a = 0;
    while (a < 10) {
        
        int b = 0;
        while (b < 10) {
            
            int c = 0;
            while (c < 10) {
                
            int abc = a * 100 + b * 10 + c;

                int d = 0;
                while (d < 10) {
                    
                    int e = 0;
                    while (e < 10) {
                        
                    int dbe = d * 100 + b * 10 + e;
                        
                        int f = 0;
                        while (f < 10) {    
                            
                        int eff = e * 100 + f * 10 + f;
                            
                            int g = 0;
                            while (g < 10) {
                            
                            int deg = d * 100 + e * 10 + g;
                            
                                int ffa = f * 100 + f * 10 + a;
                                
                                    int h = 0;
                                    while (h < 10) {
                                    
                                        int i = 0;
                                        while (i < 10) {
                                        
                                        int hfi = h * 100 + f * 10 + i;
                                        
                                        int cbi = c * 100 + b * 10 + i;
                                            
                                        int fed = f * 100 + e * 10 + d;
                                                
                                        int j = 0;
                                        while (j < 10) {
                                                    
                                        int dej = d * 100 + e * 10 + j;
                                                    
                                            if (abc + dbe == eff &&
                                                deg + ffa == hfi &&
                                                cbi + fed == dej &&
                                                abc - deg == cbi &&
                                                dbe - ffa == fed &&
                                                eff - hfi == dej) {
                                                    
                                                         
                                                 System.out.println(abc);
                                                 System.out.println(dbe);
                                                 System.out.println(eff);
                                                 System.out.println(deg);
                                                 System.out.println(ffa);
                                                 System.out.println(hfi);
                                                 System.out.println(cbi);
                                                 System.out.println(fed);
                                                 System.out.println(dej);
                                                }
                                            }
                                            j++;
                                        }
                                        i++;            
                                    }
                                    h++;
                                }   
                                g++;
                            }
                            f++;  
                        }
                        e++; 
                    }
                    d++; 
                }
                c++;
            }
            b++;
        }
        a++;
    }
}

I expect the program to output the values for the combined digits (i.e. abc, dbe, ...), instead the program outputs a never ending stream of seemingly random numbers.

Can you help me find a working solution?

Upvotes: 0

Views: 47

Answers (1)

Scary Wombat
Scary Wombat

Reputation: 44834

For a start j++ is outside of the while (j < 10) { loop, so it will continue for ever. This is the same for all other increments.

Also in the first instance, the all the values will be zero and thus equals

output (removing zeros)

602
309
911
398
116
514
204
193
397

Upvotes: 1

Related Questions