0

我有这个 java 程序,它应该显示三角形、梯形和六边形的几何形状的边数。我设法创建了代码,这样当我运行 java Shape 时,它​​会同时显示所有形状和侧面,但我想让它具有交互性,以便可以提示用户输入她想要的形状并返回它的边数,Java 中的哪个功能最能做到这一点?

public abstract class Shape {

    public abstract void numberOfSides();

    public static void main(String[] args)
    {
        System.out.println("The Geometrical characteristics of the figures are as follows");

        Trapezoid t=new Trapezoid();
        Triangle tg=new Triangle();
        Hexagon h=new Hexagon();
        t.numberOfSides();
        tg.numberOfSides();
        h.numberOfSides();
    }
}
class Trapezoid extends Shape {
    public void numberOfSides()
    {
        System.out.println("Trapezoid~It is geometrical figure with an attribute of 4 sides (Of which two are parallel and with no angles)");
    }
}
class Triangle extends Shape {
    public void numberOfSides()
    {
        System.out.println("Triangle~It is geometrical figure with an attribute of 3 sides");   
    }
}   
class Hexagon extends Shape {
    public void numberOfSides()
    {   
        System.out.println("Hexagon-It is geometrical figure with an attribute of 6 sides");
    }
}

这是我在上面尝试实施后的新线,它说存在错误,过时的方法..

import java.io.*;
 public abstract class Shape {

    public abstract void numberOfSides();

    public static void main(String[] args) 
    {  
        System.out.println("Enter the name of the Shape");
        BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
        try {
        String shapeName = br.readLine();

        if (br.equals ("Trapezoid"))
                {Trapezoid t = new Trapezoid();
                t.numberOfSides();
                }
        else if (br.equals ("Triangle"))    
                {Triangle tg = new Triangle();
                tg.numberOfSides();
                }
        else if (br.equals ("Hexagon")) 
                {Hexagon h = new Hexagon();
                h.numberOfSides();
                }
                }    
}
class Trapezoid extends Shape {
    public void numberOfSides()
{
        System.out.println("Trapezoid~It is geometrical figure with an attribute of 4 sides (Of which two are parallel and with no angles)");
}
}
class Triangle extends Shape {
    public void numberOfSides()
{
        System.out.println("Triangle~It is geometrical figure with an attribute of 3 sides");   
}
}   
class Hexagon extends Shape {
    public void numberOfSides()
{   
        System.out.println("Hexagon-It is geometrical figure with an attribute of 6 sides");
}
}
4

1 回答 1

1

要从用户那里读取输入,您需要使用 Buffered Reader。然后,您需要根据用户输入有条件地调用您的方法。

前任。

System.out.println("Enter the name of the shape");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String shapeName = br.readLine();

if(br.equals("Trapezoid") {
   Trapezoid t = new Trapezoid();
   t.numberOfSides();
}
//...

一旦您编写了该代码,您就可以实现一个菜单供用户选择他们想要的形状。代码看起来像这样。

System.out.println("Select one of the following shapes. \n 1) Trapezoid \n 2) Triangle \n 3) Hexagon");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String shapeName = br.readLine();

if(br.equals("Trapezoid" || br.equals("1") {
   Trapezoid t = new Trapezoid();
   t.numberOfSides();
}
// ...
于 2013-03-25T17:47:06.937 回答