Java,对象编程:获取返回0值的方法
主类Java,对象编程:获取返回0值的方法,java,object,Java,Object,主类 import javax.swing.JOptionPane; public class Shapes { static String shape; static String circleInput; static String c; static String r; static String t; static double radius; publi
import javax.swing.JOptionPane;
public class Shapes {
static String shape;
static String circleInput;
static String c;
static String r;
static String t;
static double radius;
public static void main(String[] args) {
// Creation of all three shape objects
Circle circleSolution = new Circle();
Rectangle solution2 = new Rectangle(shape, r);
Triangle solution3 = new Triangle(shape, t);
String choice = "y";
// Creation of a while loop which continues when 'choice' is 'y'
while (choice.equalsIgnoreCase("y")) {
// Prompts user for a shape or the interger 1 to end the program.
shape=
JOptionPane.showInputDialog ("Enter one of the following shapes:"
+ " circle, rectangle or triangle.");
// Converts the String 'shape' into an interger
if (shape.equalsIgnoreCase("circle")) {
shape = c;
circleInput = JOptionPane.showInputDialog ("Enter the radius.");
radius = Math.abs(Integer.parseInt(circleInput));
JOptionPane.showMessageDialog (
null, "The area of the circle is " + circleSolution.getCircleArea(), "Results",
JOptionPane.PLAIN_MESSAGE);
JOptionPane.showMessageDialog (
null, "The perimeter of the circle is " + circleSolution.getCirclePerimeter(), "Results",
JOptionPane.PLAIN_MESSAGE);
}
else if (shape.equalsIgnoreCase("rectangle")) {
shape = r;
// Prompts user for pertinent data
}
else if(shape.equalsIgnoreCase("triangle")) {
shape = t;
}
}
public class Circle {
// Circle object created
Shapes circle = new Shapes();
// Defines variables used in class
private static String shape;
private static double radius;
private static String circleInput;
private static String c;
// Constructor for circles, with arguments
Circle() {
shape = Shapes.shape;
radius = Shapes.radius;
circleInput = Shapes.circleInput;
c = Shapes.c;
}
// Instance method used to return the shape
public String getShape() {
return shape;
}
// Instance method used to return the value of the circle's radius
public double getRadius() {
return radius;
}
// Instance method used return the string, circleInput
public String getCircleInput() {
return circleInput;
}
// Instance method used to return the specific shape
public String getC() {
return c;
}
double getCircleArea() {
double circleArea = Math.PI * radius * radius;
return circleArea;
}
double getCirclePerimeter() {
double circlePerimeter = 2 * Math.PI * radius;
return circlePerimeter;
}
}
圆形对象类
import javax.swing.JOptionPane;
public class Shapes {
static String shape;
static String circleInput;
static String c;
static String r;
static String t;
static double radius;
public static void main(String[] args) {
// Creation of all three shape objects
Circle circleSolution = new Circle();
Rectangle solution2 = new Rectangle(shape, r);
Triangle solution3 = new Triangle(shape, t);
String choice = "y";
// Creation of a while loop which continues when 'choice' is 'y'
while (choice.equalsIgnoreCase("y")) {
// Prompts user for a shape or the interger 1 to end the program.
shape=
JOptionPane.showInputDialog ("Enter one of the following shapes:"
+ " circle, rectangle or triangle.");
// Converts the String 'shape' into an interger
if (shape.equalsIgnoreCase("circle")) {
shape = c;
circleInput = JOptionPane.showInputDialog ("Enter the radius.");
radius = Math.abs(Integer.parseInt(circleInput));
JOptionPane.showMessageDialog (
null, "The area of the circle is " + circleSolution.getCircleArea(), "Results",
JOptionPane.PLAIN_MESSAGE);
JOptionPane.showMessageDialog (
null, "The perimeter of the circle is " + circleSolution.getCirclePerimeter(), "Results",
JOptionPane.PLAIN_MESSAGE);
}
else if (shape.equalsIgnoreCase("rectangle")) {
shape = r;
// Prompts user for pertinent data
}
else if(shape.equalsIgnoreCase("triangle")) {
shape = t;
}
}
public class Circle {
// Circle object created
Shapes circle = new Shapes();
// Defines variables used in class
private static String shape;
private static double radius;
private static String circleInput;
private static String c;
// Constructor for circles, with arguments
Circle() {
shape = Shapes.shape;
radius = Shapes.radius;
circleInput = Shapes.circleInput;
c = Shapes.c;
}
// Instance method used to return the shape
public String getShape() {
return shape;
}
// Instance method used to return the value of the circle's radius
public double getRadius() {
return radius;
}
// Instance method used return the string, circleInput
public String getCircleInput() {
return circleInput;
}
// Instance method used to return the specific shape
public String getC() {
return c;
}
double getCircleArea() {
double circleArea = Math.PI * radius * radius;
return circleArea;
}
double getCirclePerimeter() {
double circlePerimeter = 2 * Math.PI * radius;
return circlePerimeter;
}
}
对于圆的面积和周长,我得到的结果都是0.0。显然,这是不可取的
如果你有任何问题,请尽管问
任何和所有的帮助/建议将不胜感激,提前感谢 您的问题是没有在
圆
对象实例中指定半径值。获取输入,解析它,然后调用getCircleArea()
似乎您正试图让Circle的构造函数使用主类中的静态radius变量作为半径
我不想解释为什么这不好(完全违背OOP范式),但这不起作用,因为在创建对象之后分配静态变量
if (shape.equalsIgnoreCase("circle")) {
shape = c;
circleInput = JOptionPane.showInputDialog ("Enter the radius.");
radius = Math.abs(Integer.parseInt(circleInput));
circleInput.setRadius(radius); //DO THIS
JOptionPane.showMessageDialog (
null, "The area of the circle is " + circleSolution.getCircleArea(), "Results",
JOptionPane.PLAIN_MESSAGE);
类的静态成员是默认初始化的
static double radius;
在这种情况下,半径
初始化为零。这是用来计算面积和周长的
另外注意:实例化包含应用程序主方法的类是非常罕见的。好吧,代码有几个问题。可能是因为您误解了静态和一些面向对象的概念 您的问题:
class Circle{
private double radius;
}
public class Shapes {
private double radius;
}
class Circle extends Shapes{
//radius inherited from Shapes now
}
public class Shapes {
private double radius;
}
class ShapesRunner(){ //Have a designated class for testing
public static void main(String[] args){
String circleInput, c, r, t;
//Your testing for the created shapes.
}
}
//receive radius from input
circle c = new Circle(radius);
//print c's area and parameter
如果未使用setter设置对象的值,则至少需要一个参数化构造函数来构造对象:
// Constructor for circles, with arguments
Circle(double radius)
{
radius = this.radius;
//Your other variables such as c & circleInput variable is not supposed to be here
}
class Circle{
private double radius;
}
public class Shapes {
private double radius;
}
class Circle extends Shapes{
//radius inherited from Shapes now
}
public class Shapes {
private double radius;
}
class ShapesRunner(){ //Have a designated class for testing
public static void main(String[] args){
String circleInput, c, r, t;
//Your testing for the created shapes.
}
}
//receive radius from input
circle c = new Circle(radius);
//print c's area and parameter
class Circle{
private double radius;
}
public class Shapes {
private double radius;
}
class Circle extends Shapes{
//radius inherited from Shapes now
}
public class Shapes {
private double radius;
}
class ShapesRunner(){ //Have a designated class for testing
public static void main(String[] args){
String circleInput, c, r, t;
//Your testing for the created shapes.
}
}
//receive radius from input
circle c = new Circle(radius);
//print c's area and parameter
circleInput
)与其他实例变量(如radius)混合使用。您还在yourshape类中编写main方法,所有变量都混在一起。你的学校可能希望你把他们分开class Circle{
private double radius;
}
public class Shapes {
private double radius;
}
class Circle extends Shapes{
//radius inherited from Shapes now
}
public class Shapes {
private double radius;
}
class ShapesRunner(){ //Have a designated class for testing
public static void main(String[] args){
String circleInput, c, r, t;
//Your testing for the created shapes.
}
}
//receive radius from input
circle c = new Circle(radius);
//print c's area and parameter
Java,对象编程:获取返回0值的方法
确保使用setter(如果有)或通过其构造函数设置圆的半径
示例:
class Circle{
private double radius;
}
public class Shapes {
private double radius;
}
class Circle extends Shapes{
//radius inherited from Shapes now
}
public class Shapes {
private double radius;
}
class ShapesRunner(){ //Have a designated class for testing
public static void main(String[] args){
String circleInput, c, r, t;
//Your testing for the created shapes.
}
}
//receive radius from input
circle c = new Circle(radius);
//print c's area and parameter
我的魔法球是说你犯了一些错误;)为什么使用静态字段?如何传递半径的值。您必须通过方法params获取它,或者重新定义构造函数来获取它。
radius=Shapes.radius这个看起来像罪犯。在创建圆实例形状时调试并检查值。半径
为0。如果随后更改静态值,则圆将不会更新。在知道值后创建圆,或者直接更新圆实例。我相信这比一个魔术球要有用一些……谢谢你的帮助,基本上重写了代码以删除所有静态变量。但是,仍然遇到了一个问题。我在Circle类中创建了一个setRadius方法,但是,我得到了一个错误,它无法在我的main方法中找到符号。public double-setRadius(double-radius){return radius;}它应该是public void-setRadius(double-radius){this.radius=radius;}
您正在为类成员赋值radius
,而不是检索它谢谢!我真的需要温习一下,当然还要练习。它现在工作:)@Goddard您可以选择要接受的答案(通过单击空心勾号)。但你只能选择一个。因此,您可能希望接受对您最有帮助的解决方案。谢谢!信息量大,很有帮助。我的代码确实帮了我一些可怕的问题。