-3

I'm still starting to learn methods in Java, anyone know how to pass on the information into the area variable?

import java.math.*;

public class AreaCircle {
    static double radius;
    static double area;

    
    public static void circleInput() {
        Scanner sc = new Scanner(System.in);    
        System.out.println("Radius: ");
        double radius = sc.nextDouble();
        
        
    }
    public static void processing() {
        double area = Math.PI * (radius * radius);
        
    }
    public static void output() {
        System.out.println("Area: "+area);
    }
    
    
    public static void main(String args[]) {
        
        circleInput();
        
        processing();
        output();
    }

}

Output

Area: 0.0

I am just doing a simple code to calculate the area of a circle, and I want to have three methods in the code, the input, processing, and output.

  • 3
    Related: [What is 'scope' in Java?](https://stackoverflow.com/questions/38177140/what-is-scope-in-java) – MC Emperor Nov 04 '21 at 17:10
  • 1
    The right way to organize this isn't to try to share variables, but to pass their values along: `double radius = sc.nextDouble(); double area = computeArea(radius); output(area);`, and to remove every variable that you have marked `static`. – Louis Wasserman Nov 04 '21 at 17:29

1 Answers1

1

In your processing() method, remove double. It's creating an variable that only exists in you method. Without double it is actually using your member area.