英文:
I faced an error while creating a circle object in my driver code
问题
以下是翻译好的部分:
// 这是我的方法和数据字段的代码
public class Circle {
private final double PI = 3.14159;
private double radius;
public Circle(double rad) {
radius = rad;
}
public void setRadius(double rad) {
radius = rad;
}
public double getRadius() {
return radius;
}
public double getArea() {
return PI * radius * radius;
}
public double getDiameter() {
return radius * 2;
}
public double getCircumference() {
return 2 * PI * radius;
}
}
// 这是我的驱动程序代码
public class Mainclass {
public static void main(String... args) {
// 创建一个用于键盘输入的 Scanner 对象。
Scanner keyboard = new Scanner(System.in);
// 从用户获取半径;
System.out.print("Enter the radius of your circle:");
double radius = keyboard.nextDouble();
// 创建一个圆对象。
Circle circle = new Circle(radius);
// 从圆获取数据并显示
System.out.println("The circle's area is: " + circle.getArea());
System.out.println("The circle's diameter is: " + circle.getDiameter());
System.out.println("The circle's circumference is: " + circle.getCircumference());
}
}
英文:
While making a circle class program. I faced an error in my driver section. The error is, Exception in thread "main" java.lang.Error: Unresolved compilation problems:
LastLab cannot be resolved to a type
LastLab cannot be resolved to a type
at Task1.Mainclass.main(Mainclass.java:18)
This is my method and data field code
public class Circle {
private final double PI = 3.14159;
private double radius;
public void Circle(double rad) {
radius = rad;
}
public void setRadius(double rad) {
radius = rad;
}
public double getRadius() {
return radius;
}
public double getArea() {
return PI * radius * radius;
}
public double getDiameter() {
return radius * 2;
}
public double getCircumference() {
return 2 * PI * radius;
}
}
This is my driver code
public class Mainclass {
public static void main(String... args) {
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Get the radius from user;
System.out.print("Enter the radius of your circle:");
double radius = keyboard.nextDouble();
// Create a circle object.
LastLab circle = new LastLab();
// Get data from circle and display it
System.out.println("The circle's area is: " + circle.getArea());
System.out.println("The circle's diameter is: " + circle.getDiameter());
System.out.println("The circle's circumference is: " + circle.getCircumference());
}
}
答案1
得分: 0
Circle circle = new Circle();
你的声明有误:`LastLab circle = new LastLab();`
而且你错误地声明了 `Circle` 的构造函数。请查看以下内容:
public class Circle {
private static final double PI = 3.14159;
private final double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public double getArea() {
return PI * radius * radius;
}
public double getDiameter() {
return radius * 2;
}
public double getCircumference() {
return 2 * PI * radius;
}
}
public class Mainclass {
public static void main(String... args) {
Scanner sca = new Scanner(System.in);
System.out.print("Enter the radius of your circle: ");
Circle circle = new Circle(sca.nextDouble());
System.out.format(Locale.ENGLISH, "The circle's area is: %.2f\n", circle.getArea());
System.out.format(Locale.ENGLISH, "The circle's diameter is: %.2f\n", circle.getDiameter());
System.out.format(Locale.ENGLISH, "The circle's circumference is: %.2f\n", circle.getCircumference());
}
}
英文:
Circle circle = new Circle();
You have incorrect declaration: LastLab circle = new LastLab();
And you have incorrectly declared constructor for Circle
. Check this out:
public class Circle {
private static final double PI = 3.14159;
private final double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public double getArea() {
return PI * radius * radius;
}
public double getDiameter() {
return radius * 2;
}
public double getCircumference() {
return 2 * PI * radius;
}
}
public class Mainclass {
public static void main(String... args) {
Scanner sca = new Scanner(System.in);
System.out.print("Enter the radius of your circle: ");
Circle circle = new Circle(sca.nextDouble());
System.out.format(Locale.ENGLISH, "The circle's area is: %.2f\n", circle.getArea());
System.out.format(Locale.ENGLISH, "The circle's diameter is: %.2f\n", circle.getDiameter());
System.out.format(Locale.ENGLISH, "The circle's circumference is: %.2f\n", circle.getCircumference());
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论