gpt4 book ai didi

Java程序以交互方式显示几何图形中的形状数量

转载 作者:行者123 更新时间:2023-12-01 14:44:11 26 4
gpt4 key购买 nike

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

最佳答案

要读取用户的输入,您需要使用缓冲读取器。然后您需要根据用户输入有条件地调用您的方法。

例如。

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();
}
// ...

关于Java程序以交互方式显示几何图形中的形状数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15621390/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com