尝试获取输入并实现另一个类的方法

问题描述

我正在做一个学校项目,很忙。我正在尝试从shape2d类(具体来说是圆形)实现方法,以使用户输入“半径”为一个圆,从而将圆的面积放出。如果我能确定一种形状,则可以申请其余的形状。


package shape;

import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
import shape.shape2d.Circle;

public class shapeMenu {
    public static void main(String[] args) {
                Date date = new Date(); 
                Calendar cal = Calendar.getInstance();
         
        Scanner in = new Scanner(system.in);                
        System.out.println("*********Welcome to the Java OO Shapes Program **********");
                System.out.println("Select from the menu below:");
                System.out.println("1. Construct a Circle");
                System.out.println("2. Construct a Rectangle");
                System.out.println("3. Construct a Square");
                System.out.println("4. Construct a Triangle");
                System.out.println("5. Construct a Sphere");
                System.out.println("6. Construct a Cube");
                System.out.println("7. Construct a Cone");
                System.out.println("8. Construct a Cylinder");
                System.out.println("9. Construct a Torus");
                System.out.println("10. Exit the program");
        
        boolean quit = false;
        int selection;
        do {
            selection = in.nextInt();
            switch (selection) {
            case 1: //1. Construct a Circle
                System.out.println("What is the radius?");
                                double rad = Double.parseDouble(in.nextLine());
                                Circle c = shape2d.Circle(rad);             
                                System.out.println("The area of the circle is " + c + ".");
                break;
            case 2: //2. Construct a Rectangle
                System.out.println("What is the length?");
                                
                                System.out.println("What is the width?");
                // do something...
                break;
            case 3: //3. Construct a Square
                System.out.println("What is the length?");
                // do something...
                break;
            case 4: //4. Construct a Triangle
                System.out.println("What is the base length?");
                                
                                System.out.println("What is the height?");
                // do something...
                break;
            case 5: //5. Construct a Sphere
                System.out.println("What is the radius?");
                // do something...
                break;                                
                        case 6: //6. Construct a Cube
                System.out.println("What is the length?");
                // do something...
                break;
                        case 7: //7. Construct a Cone
                System.out.println("What is the radius?");
                                
                                System.out.println("What is the height?");
                // do something...
                break;                                                                                               
                        case 8: //8. Construct a Cylinder
                System.out.println("What is the radius?");
                                
                                System.out.println("What is the height?");
                // do something...
                break;                                
                         case 9: //9. Construct a Torus
                System.out.println("What is the radius major?");
                                
                                System.out.println("What is the radius minor?");
                // do something...
                break;
                                                                
            case 0:  //10. Exit the program
                quit = true;
                break;
            default:
                System.out.println("Invalid choice.");
            }
        } while (!quit);
        System.out.println("Thanks for using the program. Today is " + date + ".");
    }
}

这将启动菜单,我创建了具有单独形状(圆形,三角形等)的shape2d和shape3d类,以获取面积和体积。

package shape;

public class shape2d extends shapeMenu {
    public double a;

    public static class Circle extends shape2d{
        private double r;
        public Circle(double r){
            this.r = r;
            
        }
        public void area(double r){
            a = Math.PI * Math.pow(r,2);
        }
    }    

    public class Rectangle extends shape2d{
        private double l;
        private double w;
        public Rectangle(double l,double w){
            a = l * w;
        }
    }
    
    public class Square extends shape2d{
        private double l;
        public Square(double l){
            a =  Math.pow(l,2);
        }

    }
    public class Triangle extends shape2d{
        public double b;
        public double h;
        public Triangle(double b,double h){
            a = b * h / 2;
        }
    }
}   

我现在有点精疲力尽....会睡一会儿再回到那里。谢谢您的光临!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)