java - the code won't work, calculator -


i try run program multiple times, won't run.

"it looks post code; please add more details."


public class calculator {     private static final int add = 0;     private static final int sub = 1;     private int a;     private int b;     public int geta() {       return a; }     public void seta(int a) {       this.a = a;}     public int getb() {       return b;     }     public void setb(int b) {       this.b = b;}     public int calculate(int type) {       calculator meincalc = new calculator();       if(type == add){           int r = meincalc.geta() + meincalc.getb();           return r;       }       if(type == sub){           int r = meincalc.geta() - meincalc.getb();           return r;       }       return meincalc.calculate(type);     }   public static void main(int[] args) {     calculator meincalc = new calculator();     meincalc.seta(12);     meincalc.setb(8);     system.out.println(meincalc.calculate(add));     system.out.println(meincalc.calculate(sub));   } } 

what can do? dont know..

the following should work although isn't cleanest solution. removed 2 constants. weren't needed , if set different values, lead confusion. split calculate() 2 different functions add() , sub() each 1 thing. string in prints aren't needed. can have system.out.println(meincalc.add()) if prefer.

public class calculator {     private int a;     private int b;      public static void main(string[] args) {         calculator meincalc = new calculator();         meincalc.seta(12);         meincalc.setb(8);         system.out.println("add " + meincalc.add());         system.out.println("sub " + meincalc.sub());     }       public int geta() {         return a;     }     public void seta(int a) {         this.a = a;     }      public int getb() {         return b;     }     public void setb(int b) {         this.b = b;     }      public int add() {         return geta() + getb();     }       public int sub() {         return geta() - getb();     } } 

edit:

if must use calculate():

public class calculator {     private static final int add = 0;     private static final int sub = 1;     private int a;     private int b;      public static void main(string[] args) {         calculator meincalc = new calculator();         meincalc.seta(12);         meincalc.setb(8);         system.out.println("add " + meincalc.calculate(0));         system.out.println("sub " + meincalc.calculate(1));     }       public int geta() {         return a;     }     public void seta(int a) {         this.a = a;     }      public int getb() {         return b;     }     public void setb(int b) {         this.b = b;     }      public int calculate(int type) {         int sum = 0;         if (type == add){             sum = geta() + getb();         }         if (type == sub){             sum = geta() - getb();         }         return sum;     }  } 

i highly suggest against not using if want wrap head around went wrong, there go.


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -