我有这个 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");
}
}