如何更改程序的输出以便可以先输入所有输入,然后输出所有输出

时间:2018-03-30 17:20:00

标签: java arrays input output

当我立即运行程序时,输出会在每次输入后直接输出。因此,如果我输入我要输入3个形状,它将要求输入第一个形状,然后立即给出该个别形状的输出,然后请求第二个形状的输入,依此类推。

我想首先要求所有形状/参数的所有输入,然后分别给出所有输出。它只是if语句的放置问题吗?无论出于何种原因,我似乎无法弄清楚我在这里做错了什么。

import java.util.Scanner;

public class TestShapes {

public static void main(String args[]) {

    Scanner scan = new Scanner(System.in);
    System.out.print("Enter number of shapes: ");
    int num = scan.nextInt();

 Shape[] shape = new Shape[num];

    for(int i = 0;i < num;i++){
        System.out.print("Enter the choice (Square, Rectangle, or Circle): ");       
        int shapeType = scan.nextInt();

        if(shapeType == 1){
                System.out.print("Enter the color: ");
                String color = scan.next();
                System.out.print("Enter the side length of the square: ");
                double sideLength = scan.nextDouble();
                Square sq = new TestShapes(). new Square(color,sideLength);
                shape[i] = sq;
                shape[i].print();
        }
        else if(shapeType == 2){
                System.out.print("Enter the color: ");
                String color = scan.next();
                System.out.print("Enter the length of the rectange: ");
                double length = scan.nextDouble();
                System.out.print("Enter the width of the rectange: ");
                int width = scan.nextInt();
                Rectangle rc = new TestShapes(). new Rectangle(color,length,width);
                shape[i] = rc;
                shape[i].print();
        }
        else if(shapeType == 3){
                System.out.print("Enter the color: ");
                String color = scan.next();
                System.out.print("Enter the radius of the circle: ");
            double radius = scan.nextDouble();
            Circle cr = new TestShapes(). new Circle(color,radius);
            shape[i] = cr;
            shape[i].print();
        }
    }

}

class Shape{
    String color;
    public Shape(){
        color = "red";
    }

    public Shape(String c){
        color = c;
    }

    public void setColor(String c){
        this.color = c;
    }

    public String getColor(){
        return this.color;
    }

    public void print(){
        System.out.println("Color: " +color);
    }

    public double area() {
return 0;
 }
}

class Square extends Shape{

    double sideLength;

    public Square(){
        super("red");
        sideLength = 1;
    }

    public Square(String color,double sLength){
        super(color);
        sideLength = sLength;
    }

    public void setSideLength(double sl){
        this.sideLength = sl;
    }

    public double getSideLength(){
        return this.sideLength;
    }

    public void print(){
        super.print();
        System.out.println("Side Length: " + sideLength);
        System.out.println("Area: " + area());
    }

    public double area(){
        return sideLength * sideLength;
    }
}


class Circle extends Shape{

    double radius;

    public Circle(){
        super("red");
        radius = 1;
    }

    public Circle(String color,double radius){
        super(color);
        this.radius = radius;
    }

    public void setRadius(double r){
        this.radius = r;
    }

    public double getRadius(){
        return this.radius;
    }

    public void print(){
        super.print();
        System.out.println("Radius of the circle: " + radius);
        System.out.println("Area of the circle: " + area());
    }

    public double area(){
        return 3.14 * radius * radius;
    }
}

class Rectangle extends Shape{

    double width;
    double length;

    public Rectangle(){
        super("red");
        length = 1;
        width = 1;
    }

    public Rectangle(String color,double length,double width){
        super(color);
        this.length = length;
        this.width = width;
    }

    public void setWidth(double w){
        this.width = w;
    }

    public double getWidth(){
        return this.width;
    }

    public void setLength(double l){
        this.length = l;
    }

    public double getLength(){
        return this.length;
    }

    public void print(){
        super.print();
        //System.out.println("Side of the rectangle: " + sideLength);
        System.out.println("Area of the rectangle: " + area());
    }

    public double area(){
        return length * width;
    }
  }
}

3 个答案:

答案 0 :(得分:0)

完成所有输入后,只需调用打印方法即可。像这样的东西:

...
//start input here
for(int i = 0;i < num;i++){
    System.out.print("Enter the choice (Square, Rectangle, or Circle): ");       
    int shapeType = scan.nextInt();

    if(shapeType == 1){
            System.out.print("Enter the color: ");
            String color = scan.next();
            System.out.print("Enter the side length of the square: ");
            double sideLength = scan.nextDouble();
            Square sq = new TestShapes(). new Square(color,sideLength);
            shape[i] = sq;
    }
    else if(shapeType == 2){
            System.out.print("Enter the color: ");
            String color = scan.next();
            System.out.print("Enter the length of the rectange: ");
            double length = scan.nextDouble();
            System.out.print("Enter the width of the rectange: ");
            int width = scan.nextInt();
            Rectangle rc = new TestShapes(). new Rectangle(color,length,width);
            shape[i] = rc;
    }
    else if(shapeType == 3){
            System.out.print("Enter the color: ");
            String color = scan.next();
            System.out.print("Enter the radius of the circle: ");
        double radius = scan.nextDouble();
        Circle cr = new TestShapes(). new Circle(color,radius);
        shape[i] = cr;
    }
}
//start printing here
for(int i = 0; i < num; i++){
        shape[i].print();
}
...

答案 1 :(得分:0)

如果您想先获取输入并在之后处理它,只需使用两个循环即可。第一个读取输入并将其存储在数组中。之后,第二个循环遍历数组并执行所需的操作。

int[] shapeType = new int[num];
for (int i = 0; i < num; i++) {
     System.out.print("Enter the choice (Square, Rectangle, or Circle): ");       
     int shapeType[i] = scan.nextInt();
}
for (int j = 0; j < num; j++) {
    int shapeType = scanType[j];
    if (shapeType == 1) {
        ...

这可以进行优化,但只需对代码进行一些更改和添加即可。

答案 2 :(得分:0)

在完成所有选择之前,你正在循环中执行shape[i].print()。您可以尝试删除这些语句并在第一次for循环后立即显示所有选项(通过循环遍历形状数组)