形状工具箱获取长度和宽度的输入并打印它们

时间:2018-02-20 07:41:58

标签: java

我必须制作一个带有方形,矩形,圆形和三角形的形状工具箱。基本上用户输入形状,然后长度,高度,周长等,程序将输出形状本身加上文字说长度,宽度区域,周长等。我已经有了方形类已经有了一些帮助你真棒人们,现在我正在研究矩形类并遇到了问题。我将在这里发布我的main方法和我的矩形类,我也将发布我的方形类作为参考,因为它完美地工作。得到我的是公共Rectanglebox(int width)。当只有一个输入长度但是我试图弄清楚如何在类中添加第二个输入时,这对于square非常有效,因为这个类也需要该参数。

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.Scanner;
import javax.swing.JFrame;


public class Main {

    static int input;
    static int length;
    static int width;

    public static void main(String[] args) {

        Scanner sc1 = new Scanner(System.in);
        while(true) {
            System.out.print("\n1 for square\n2 for restangle\n3 for triangle\n4 for circle\n5 to exit\n\nWhich shape do you want? ");
            input = sc1.nextInt();
            if(input == (5)) {
                break;
            }
            if(input == (1)) {


                System.out.print("Input the length: ");
                length = sc1.nextInt();
                Square mySquare = new Square(length);

                JFrame frame = new JFrame();

                frame.setSize(300,400);
                frame.setTitle("ShapeViewer");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                frame.add(mySquare);
                frame.setVisible(true);}

            if(input==(2)) {


                System.out.print("Input the length: ");
                length = sc1.nextInt();
                System.out.print("Input the width: ");
                width = sc1.nextInt();
                Rectanglebox myRectangle = new Rectanglebox(length);

                JFrame frame2 = new JFrame();

                frame2.setSize(300,400);
                frame2.setTitle("ShapeViewer");
                frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                frame2.add(myRectangle);
                frame2.setVisible(true);
            }

            }


        }
    }
import java.util.Scanner;
import javax.swing.JComponent;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Rectangle;

public class Rectanglebox extends JComponent{

    private int length;
    private int width;

    public Rectanglebox(int length) {
        this.length = length;
    }

    public Rectanglebox(int width) {
        this.width = width;
    }

    public double getArea() {
        return length * width;
    }
    public double getPerimeter() {
        return (length * 2) + (width * 2);
    }

    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D)g;
        Rectangle square = new Rectangle(100,100,length,length);
        g2.draw(square);
        g2.drawString("Area: "+getArea(), 110, 110+length);
        g2.drawString("Perimeter: " + getPerimeter(), 200, 110+length);
        g2.drawString("Square", 110, 20);
        g2.drawString("Length: " +length, 10, 200+length);
        g2.drawString("Width" +width, 60, 200);

        g2.setColor(Color.black);
        g2.fill(square);
    }
}
import java.util.Scanner;
import javax.swing.JComponent;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Rectangle;

public class Square extends JComponent{

    private int length; 

    public Square(int length) {
        this.length = length;
    }

    public double getArea() {   
    return length * length;
    }
    public int getPerimeter() {
        return length * 4;
    }
    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D)g;
        Rectangle square = new Rectangle(50,50,length,length);
        g2.draw(square);
        g2.drawString("Area: "+getArea(), 110, 110+length);
        g2.drawString("Perimeter: " + getPerimeter(), 200, 110+length);
        g2.drawString("Square", 110, 20);
        g2.drawString("Length: " +length, 10, 110+length);
        g2.setColor(Color.black);
        g2.fill(square);
    }
}

我认为这里的主要问题是我实际上并不完全理解这句话甚至在做什么我只知道它有效。我的意思是我知道它的构造函数接受并设置长度,但我不明白它的限制,因为我不明白为什么我不能再做一个像宽度接受宽度输入的宽度输入。

public Square(int length) {
        this.length = length;
    }

1 个答案:

答案 0 :(得分:0)

您不能使用具有相同参数的两个构造函数。您必须为Rectanglebox创建第二个构造函数,例如:

public Rectanglebox(int length, int width) {
  this.length = length;
  this.width = width;
}

在你的Main.java中你必须写:

if(input==(2)) {
  System.out.print("Input the length: ");  
  length = sc1.nextInt();
  System.out.print("Input the width: ");
  width = sc1.nextInt();
  Rectanglebox myRectangle = new Rectanglebox(length, width);