当我现在运行程序时,输出直接在每个输入之后。因此,如果我输入我想输入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("Area of the rectangle: " + area());
}
public double area(){
return length * width;
}
}
}