我在驱动代码中创建圆对象时遇到了一个错误。

huangapple go评论70阅读模式
英文:

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());
}
}

huangapple
  • 本文由 发表于 2020年10月24日 01:44:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64504936.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定