Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I add two variables that are in two different methods in Java?

I need to make a program that uses two different methods. The first method adds a + b + c = d whilst the second adds x + y = z. Then it has to print out d and z as well as the sum of d + z. The program has to take input from the user.

I know that you can’t call variables outside of their methods, so is there any workaround to this? It has to follow the instructions I previously stated

I’m missing the part where I add d and z, this is my current program:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import java.util.Scanner;

public class myProgram {
    
    static void methodAB(){
        
        Scanner myScan = new Scanner(System.in);
        System.out.println("Enter A: ");
        float A = myScan.nextFloat();
        System.out.println("Enter B: ");
        float B = myScan.nextFloat();
        System.out.println("Enter C: ");
        float C = myScan.nextFloat();
        
    float D = A + B + C;
    System.out.println("Sum of MethodAB: " + D);
    
    }
     static void methodXY(){
         
        Scanner myScan = new Scanner(System.in);
        System.out.println("Enter X: ");
        float X = myScan.nextFloat();
        System.out.println("Enter Y: ");
        float Y = myScan.nextFloat();  

    float Z = X + Y;
    System.out.println("Sum of MethodXY: " + Z);
    }


    public static void main(String[] args) {
    methodAB();
    methodXY();
    //this is the part im stuck on
    //i cant call variables d and z so i cant add them together

    }
}

>Solution :

just return them and then add

public class MyProgram {
    
    static float methodAB() {
        Scanner myScan = new Scanner(System.in);
        System.out.println("Enter A: ");
        float A = myScan.nextFloat();
        System.out.println("Enter B: ");
        float B = myScan.nextFloat();
        System.out.println("Enter C: ");
        float C = myScan.nextFloat();
        
        float D = A + B + C;
        System.out.println("Sum of MethodAB: " + D);
        return D;
    }
    
    static float methodXY() {
        Scanner myScan = new Scanner(System.in);
        System.out.println("Enter X: ");
        float X = myScan.nextFloat();
        System.out.println("Enter Y: ");
        float Y = myScan.nextFloat();  

        float Z = X + Y;
        System.out.println("Sum of MethodXY: " + Z);
        return Z;
    }

    public static void main(String[] args) {
        float dResult = methodAB();
        float zResult = methodXY();

        float sumResult = dResult + zResult;
        
        System.out.println("Sum of D and Z: " + sumResult);
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading