Home > Enterprise >  What is the proper way of writing this loop problem?
What is the proper way of writing this loop problem?

Time:09-29

I'm learning loop method and I'm trying to write this flowchart into a code.flowchart

Here is my code:

public class CODES {
    
    public static void main(String[] args) {
        
        int C, O, D, E, S;
        
        C=100;
        O=5;
        D=4;
        E=7;
        S=2;

        
        while(C<E)
        {
            while(E<O) {

                E = E   C;
                C = C   1;
                O = O - 1;
            }
            System.out.println(C);
            System.out.println(O);
            System.out.println(D);
            System.out.println(E);
            System.out.println(S);
                
        }
        D = E   S;
        O = O   D;
        C = C - D;
        S = S   1;
        E = E   S;
        
        
  
    }
}

Final output should be: 12, 86, 31, 88, 7.

I need to see the final value but It's not showing anything so I'm wondering what is wrong with my while loop method. I'm still learning what kind of loops should I use and also doing nested loops.

CodePudding user response:

your loops should not be nested

public class CODES {
    
    public static void main(String[] args) {
        
        int C, O, D, E, S;
        
        C=100;
        O=5;
        D=4;
        E=7;
        S=2;
        
        while(C >= E) {
            D = E   S;
            O = O   D;
            C = C - D;
            S = S   1;
            E = E   S;
        }
        
        while (E < O) {
                E = E   C;
                C = C   1;
                O = O - 2;
            }
            
        System.out.println(C);
        System.out.println(O);
        System.out.println(D);
        System.out.println(E);
        System.out.println(S);
  
    }
}
  • Related