If you’re looking for an alternative to using JOptionPane for input, you can use Scanner, which is more commonly used in console-based Java applications. Here’s how you can modify your program to use Scanner instead of JOptionPane:
import java.util.Scanner;
public class NumericTypes {
public static void main(String[] args) {
double radius;
double volume;
double diameter;
// Use Scanner for input
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the diameter of a sphere: ");
diameter = scanner.nextDouble(); // Read diameter as double
radius = diameter / 2;
volume = (4.0 / 3.0) * Math.PI * Math.pow(radius, 3);
System.out.println("The radius for the sphere is " + radius);
System.out.println("The volume of the sphere is " + volume);
}
}
Explanation:
- This approach uses Scanner to read the input from the console instead of a GUI dialog box.
- It works similarly but allows you to interact with the program in the terminal or console.