主类中的变量将分配给子类中的变量 - 初学者

时间:2013-08-21 10:48:37

标签: java

我有这个代码,它将根据用户输入的形状找到不同形状的区域。问题是如何从主类到圆形,三角形,矩形和方形类输入输入的测量值(例如长度,宽度)?这是我的代码。

    import java.lang.Math;
    import java.util.Scanner;
    import java.text.DecimalFormat;

    class Circle{
    double radius;
    void CircleMeasurement(){
        radius = r;
    }

    double getCircleArea(){
        return(Math.PI*Math.pow(radius,2));
    }
    }

    class Triangle{
    int base, height;
    void TriangleMeasurement(){
        base = b;
        height = h;
    }
    int getTriangleArea(){
        return((base*height)/2);
    }
   }

    class Rectangle{
    int length, width;
    void RectangleMeasurement(){
        length = l;
        width = w;
    }
    int getRectangleArea(){
        return(length*width);
    }
     }

    class Square{
    int sides;
    void SquareMeasurement(){
        sides = s;
    }
    int getSquareArea(){
        return( sides * sides);
    }
   }


    class Shapes{
    public static void main(String[] args){
    String key;
    double r;
    int b, h, l, w, s;

        System.out.println("Welcome!");
        System.out.println("Choose your option:");
        System.out.println("1 - Circle, 2 - Triangle, 3 - Rectangle, 4 - Square");
        Scanner in = new Scanner(System.in);
        key = in.nextLine();


    if (key=="1" || key =="circle"){
        System.out.println("Area for Circle");
        System.out.println("Enter radius:");
        Scanner.in = new Scanner(System.in);
        r = in.nextInt;
        Circle circle1 = new Circle();
        System.out.println("The area is equal to" + circle1.getCircleArea());
    }


    else if (key == "2"){
        System.out.println("Area for Triangle");
        System.out.println("Enter base:");
        Scanner.in = new Scanner(System.in);
        b = in.nextInt;
        System.out.println("Enter height:");
        h = in.nextInt;
        Triangle triangle1 = new Triangle();
        System.out.println("The area is equal to" + triangle1.getTriangleArea());

    }

    else if (key == "3"){
        System.out.println("Area for Rectangle");
        System.out.println("Enter length:");
        Scanner.in = new Scanner(System.in);
        l = in.nextInt;
        System.out.println("Enter width:");
        w = in.nextInt;
        Rectangle rectangle1 = new Rectangle();
        System.out.println("The area is equal to" + rectangle1.getRectangleArea());
    }

    else if (key == "4"){
        System.out.println("Area for Square");
        System.out.println("Enter side:");
        Scanner.in = new Scanner(System.in);
        s = in.nextInt;
        Square square1 = new Square();
        System.out.println("The area is equal to" + square1.getSquareArea());
   }

   }
   }

5 个答案:

答案 0 :(得分:1)

您可以使用构造函数在创建对象时设置适当的变量。

Circle(int r){
 radius = r;
}

Rectangle(int l, int b){
length = l;
breadth = b;
}

Circle c = new Circle(9); //creates a new Circle with radius 9
Rectangle r = new Rectangle(2,3) //Creates a new Rectangle with length as 2, breadth as 3

也就是说,你也可以使用setter方法。

此外,使用 == 来比较字符串皱眉,通常会给你错误的结果。使用 .equals()代替。

"Circle".equals(input);

答案 1 :(得分:0)

这里要提两件事:

  1. Pass the values taken from scanner to classes as argument to method or constructor.因此可以通过方法用于进一步计算。此处使用inheritance也是一个不错的选择。
  2. Never use == for string comparision. It will not work, use .equals instead.

答案 2 :(得分:0)

您的代码中的一个问题是您使用==比较字符串。应使用equals方法比较字符串。

更改此类陈述

if (key=="1" || key =="circle"){

if (key.equals("1") || key.equals("circle")){

另一个问题是你没有在你的类中定义具有适当的arguemnts的构造函数,例如Circle,Triangle等。因为你没有设置正确的属性所以调用方法不会为你提供正确的结果。

例如,您需要在Circle类中使用构造函数来初始化半径参数:

public Circle(int r) {
    radius = r;
}

像这样创建Circle的对象,然后调用getCircleArea应该给你想要的结果:

 Circle circle1 = new Circle(r);
 System.out.println("The area is equal to" + circle1.getCircleArea());

答案 3 :(得分:0)

您可以通过多种方式设置变量值。第一种方式是构造函数,请参阅rocketboy的回答。第二个是将变量标记为私有并使用mutator methods。推荐的方式/见下面的例子:

 class Circle{
        private double radius;
        Circle(double radius){
            this.radius = radius;
        }
        public void setRadius(double radius){
            this.radius = radius;
        }
        public double getRadius(){
            return radius;
        }

答案 4 :(得分:-2)

你应该创建getter和setter。然后

 class Circle{
    double radius;
    public double getRadius(){
    return radius;
    }
    public void setRadius(double r){
    radius=r;
    }
    void CircleMeasurement(){
        radius = r;
    }
....
    r = in.nextInt;
        Circle circle1 = new Circle();
    circle1.setRadious(r);