Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何更改程序的输出,使所有的输入都可以先进行,然后给出所有的输出_Java_Arrays_Input_Output - Fatal编程技术网

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

Java 如何更改程序的输出,使所有的输入都可以先进行,然后给出所有的输出,java,arrays,input,output,Java,Arrays,Input,Output,当我现在运行我的程序时,输出直接在每个输入之后。因此,如果我输入我想输入3个形状,它会要求输入第一个形状,然后立即给出单个形状的输出,然后要求输入第二个形状,依此类推 我希望它首先要求所有形状/参数的所有输入,然后分别给出所有输出。这只是if语句的位置问题吗?不管出于什么原因,我似乎都不知道我做错了什么 import java.util.Scanner; public class TestShapes { public static void main(String args[]) {

当我现在运行我的程序时,输出直接在每个输入之后。因此,如果我输入我想输入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;
    }
  }
}
import java.util.Scanner;
公共类TestShapes{
公共静态void main(字符串参数[]){
扫描仪扫描=新扫描仪(System.in);
System.out.print(“输入形状数:”);
int num=scan.nextInt();
形状[]形状=新形状[num];
for(int i=0;i
完成所有输入后,只需调用打印方法即可。大概是这样的:

...
//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();
}
...
。。。
//从这里开始输入
for(int i=0;iint[] 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) {
        ...