【问题标题】:How to change the output of the program so that ALL input can be made first, then give all output如何更改程序的输出,以便可以先进行所有输入,然后再给出所有输出
【发布时间】:2018-09-09 17:50:21
【问题描述】:

当我现在运行我的程序时,输出直接在每个输入之后。因此,如果我输入要输入 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;
    }
  }
}

【问题讨论】:

    标签: java arrays input output


    【解决方案1】:

    在所有输入完成后调用打印方法。像这样:

    ...
    //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();
    }
    ...
    

    【讨论】:

    • 谢谢!我是 java 初学者,基本上忘记了我只是立即打印
    • 正如@YeetariVsKekmet 提到的,考虑使用 switch 语句,这样 if else 语句就没有那么大了。这使您的代码更具可读性
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

        猜你喜欢
        • 2023-04-08
        • 2021-02-21
        • 2021-05-07
        • 2013-01-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-06
        相关资源
        最近更新 更多