David Dominguez Herrera David Dominguez Herrera - 1 month ago 10
Android Question

How can I change the value of a variable with a void method?

Thank you for answering.
I'm trying to change the value of a variable with a void method.

public void function(int x){
x=x*x;
System.out.println(x);
}
public void main(String args){
int p=5;
System.out.println(p); //The output would be 5.
function(p); //The output would be 25.
System.out.println(p); //The output would be 5, again.
}


What can I do in order to change the value of "p" by calling the method?

Answer

how about just write a method and call it twice instead of writing the same code ,only diffrence i see here is multi and divi so u can do something like that

    void someFunction(boolean isDivi){
     c1 = 0;c2 = 0;c3 = 0;c4 = 0;c5 = 0;c6 = 0;c7 = 0;c8 = 0;c9 = 0;c0 = 0;
        g = 0;r = 0;counter1 = 0;counter2 = 0;counter3 = 0;counter4 = 0;counter5 = 0;
        counter6 = 0;counter7 = 0;counter8 = 0;counter9 = 0;counter0 = 0;first = -1;
        second = -1;third = -1;fourth = -1;fifth = -1;sixth = -1;seventh = -1;eighth = -1;
        ninth = -1;tenth = -1;sec = 0;w = 0;h = 0;z = 0;s = 0;
if(isDivi){
Divi = Divi+1;
}
else
{
Multi = Multi+1
}
h = 1;
        if(xx==1) {
            ope = findFirst.ope(Divi, Multi, Resta, Suma);
            xx=0;
        }
        a=findFirst.oper(Divi, Multi, Resta, Suma, a, b, ope);
        if((Divi+Multi+Suma+Resta)>1){
            int cs=Divi+Multi+Suma+Resta;
            texto3.setText(""+a);
            texto2.setText(""+cs);
            texto4.setText(""+a);
        }
    } 

and then call it once in every button

    if (v.getId() == findViewById(R.id.btnDiv).getId()) {
someFunction(true);
}
if (v.getId() == findViewById(R.id.btnMul).getId()) {
someFunction(false);
}

and define your textfields once in your class for example in your onCreate method

Comments