例如
软件包testcircle1;
导入java.util.Scanner;
公共课程TestCircle1 {
public static void main(String[] args) {
System.out.println("Welcome!!!!");
Circle c1 = new Circle( );
Scanner in= new Scanner(System.in);
System.out.println("Please enter a number");
c1.setRadius() = in.nextDouble();
System.out.println(c1.getRadius());
}
}
软件包testcircle1;
公共课圈{
public Circle() {
}
public Circle(double radius) {
}
public void setRadius(double radius) {
if (radius >= 0)
this.radius = radius;
}
public double getRadius() {
return radius;
}
}
我究竟做错了什么?
最佳答案
您需要添加以下两个。
1.添加“私人双倍半径”;在课堂上
2.在主要方法中,您需要使用c1.setRadius(in.nextDouble())
提供相同的代码
导入org.slf4j.Logger;
导入org.slf4j.LoggerFactory;
导入java.util.Scanner;
公共课程TestCircle1 {
私有静态Logger logger = LoggerFactory.getLogger(TestCircle1.class);
公共静态void main(String [] args){
System.out.println("Welcome!!!!");
Circle c1 = new Circle();
try {
Scanner in = new Scanner(System.in);
System.out.println("Please enter a number");
if (!in.hasNextDouble()) {
throw new IllegalArgumentException("Input is not a double value");
}
c1.setRadius(in.nextDouble());
System.out.println(c1.getRadius());
} catch (IllegalArgumentException e) {
logger.error(e.getMessage());
}
}
}
public class Circle {
private double radius;
public Circle() {
}
public Circle(double radius) {
}
public void setRadius(double radius) {
if (radius >= 0)
this.radius = radius;
}
public double getRadius() {
return radius;
}
}