代码之家  ›  专栏  ›  技术社区  ›  Bianca

如何更改程序的输出,使所有输入都可以先进行,然后给出所有输出

  •  -1
  • Bianca  · 技术社区  · 7 年前

    当我现在运行程序时,输出直接在每个输入之后。因此,如果我输入我想输入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 回复  |  直到 7 年前
        1
  •  0
  •   Michriko    7 年前

    完成所有输入后,只需调用print方法即可。类似这样:

    ...
    //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();
    }
    ...
    
        2
  •  0
  •   Cédric Couralet    7 年前

    你在做什么 shape[i].print() 在创建所有选项之前在循环中。您可以尝试删除这些语句,并在第一个for循环之后立即显示所有选项(通过在形状数组中循环)

        3
  •  0
  •   user5956451 user5956451    7 年前

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

    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) {
            ...
    

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