Как я могу задать пользователю радиус? Я уверен, что это будет легкое решение, но я просто не знаю, как это сделать. Вот мой код до сих пор.Запрашивать ввод пользователя с помощью java-сеттеров и геттеров
import java.util.Scanner;
public class CircleDriver
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
Circle circle1 = new Circle();
circle1.setRadius(20);
System.out.println("Enter the radius of a circle " + circle1.getRadius());
System.out.println("Area = " + circle1.calculateArea());
System.out.println("Diameter = " + circle1.calculateDiameter());
System.out.println("Circumference = " + circle1.calculateCircumference());
}
}
класс Circle:
public class Circle
{
private double radius;
private final double PI = 3.14159;
/**
* this method calculates the area of the given radius
* @return
*/
public double calculateArea() {
double area;
area = (PI * radius * radius);
return area;
}
/**
* this method calculates the diameter of the given radius
* @return
*/
public double calculateDiameter() {
double diameter;
diameter = (radius * 2);
return diameter;
}
/**
* this method calculates the circumference of the given radius
* @return
*/
public double calculateCircumference() {
double circumference;
circumference = (2 * PI * radius);
return circumference;
}
/**
* this method sets the radius of the object
* @param radius
*/
public void setRadius(double radius) {
this.radius = radius;
}
/**
* this radius returns the radius given
* @return
*/
public double getRadius() {
return radius;
}
}
Все, что вам нужно сделать, это circle1.setRadius (input.nextDouble()); – StackFlowed
У вас есть «Сканер», почему вы его не используете? – RealSkeptic