samedi 23 janvier 2021

Command line calculator with java. The for loop is skipping the number that is behind it and not adding it. example 2+2+4+2 it would skip 4

The for loop is skipping the number that is behind it and not adding it. example 2+2+4+2 it would skip 4.

for(int i = 0; i < parts.length; i++) {
        int above2 = Integer.parseInt(parts[i]);
            
                if(str.contains("+")) {
                
                System.out.println(FirstNum + SecNum + above2); 
                
                }
                else if(str.contains("*")) {
                
                System.out.println(FirstNum * SecNum);
                
                }
                else if(str.contains("-")) {
                
                System.out.println(FirstNum - SecNum);
                
                }
                else if(str.contains("/") || str.contains("÷")) {

                System.out.println((FirstNum / SecNum));
                
                }
            
        }

Aucun commentaire:

Enregistrer un commentaire