简体   繁体   中英

How can I correct this switch case in Java?

So I've been working on this code for a while and it has me a bit lost. Keep in mind I am extremely new to Java and so I'm a bit slow on the uptake. I have created shape classes that implement an interface with getArea, getPerimeter and getDescription methods. There are multiple shapes but that isn't really where the problem is. The problem comes when I attempt to implement switch cases to allow the user to choose which shape he or she wants to add. I am getting the same message as many times as the Array of shapes will allow the address of the Shape I try to add. I realize the mistake I'm making is most likely a beginner one by I would really appreciate some help, Thank you. Also if you could give me a clue on how to sort the Shapes by their Area it would be greatly appreciated.

public class ShapeApp2 {

/**
 * @param args the command line arguments
 */

public static void main(String[] args) {
     Shape[] test = new Shape[10];
     System.out.println("Choose a shape or type stop to break away?");
     Scanner sc = new Scanner(System.in);
     String Shape = sc.nextLine();

     for (int i=0; i<test.length; i++) {

     switch (Shape) {
         case "Rectangle":
             System.out.println("You have chosen a Rectangle");
             test[i] = new Rectangle();
             System.out.println("Enter another one now");
             break;
         case "Square":
             System.out.println("You have chosen a Square");
             test[i] = new Square();
             System.out.println("Enter another one now");
             break;         
         case "Equilateral Triangle":
             System.out.println("You have chosen an Equilateral Triangle");
             test[i] = new Equilateral_Triangle();
             System.out.println("Enter another one now");
             break;
         case "Right Triangle":
             System.out.println("You have chosen a Right Triangle");
             test[i] = new Right_Triangle();
             System.out.println("Enter another one now");
             break;
         case "Isosceles Triangle":
             System.out.println("You have chosen an Isosceles Triangle");
             test[i] = new Isosceles_Triangle();
             System.out.println("Enter another one now");
             break;
         case "Scalene Triangle":
             System.out.println("You have chosen a Scalene Triangle");
             test[i] = new Scalene_Triangle();
             System.out.println("Enter another one now");
             break;
         case "Stop":
             break;
      }
         System.out.println(test[i]);
     }

}

}

Also here's a couple of the Shape Classes for context.

package shapeapp2;

/**
 *
 * @author my-pc
 */
public class Rectangle implements Shape {
    private double length;
    private double width;
    private String shapeName;
    public Rectangle(){
        length = 4.0;
        width = 5.0;
        shapeName = "Rectangle";
    }
     public double getArea(){
         double Area;
         Area = length * width;
         return Area;
     }
     public double getPerimeter() {
         double Perimeter;
         Perimeter = (2*length) + (2*width);
         return Perimeter;
     }
     public String getDescription() {
         return shapeName;
     }
}



 package shapeapp2;

/**
 *
 * @author my-pc
 */
public class Square implements Shape {
    private double length;
    private double width;
    private String shapeName;
    public Square(){
        length = 8.0;
        width = 8.0;
        shapeName = "Square";
    }
     public double getArea(){
         double Area;
         Area = length * width;
         return Area;
     }
     public double getPerimeter() {
         double Perimeter;
         Perimeter = (2*length) + (2*width);
         return Perimeter;
     }
     public String getDescription() {
         return shapeName;
     }
}

You are only reading the shape from the user once before the loop. You wanted to read it in the loop. That's the only thing that would make sense.

String Shape = sc.nextLine();
for (int i=0; i<test.length; i++) {

should be

for (int i=0; i<test.length; i++) {
    String Shape = sc.nextLine();

Also, you should rename that variable. Shape looks like a classname.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM