所以我已经学习 Java 大约 8 周了,在课堂上我不得不设计一个形状猜谜游戏。是的,这是家庭作业。所以我用下面的一个例子构建了我的四个形状类。
public class square extends shape {
//square member variables
boolean equalSides = true;
// check if sides are equal
public boolean isEqual() {
return equalSides;
}
//constructor
public square(int numsides, String shapeName, boolean b, String shapehint) {
super(numsides, shapeName, shapehint);
}
}
然后我创建了一个 shape.java 类
public class shape {
int numSides;
String shapeName;
String shapeHint;
public shape(int numsides, String shapename, String shapehint) {
numSides = numsides;
shapename = shapeName;
shapehint = shapeHint;
}
//getter methods
public int getSides() {
return numSides;
}
public String getName(){
return shapeName;
}
public String getHint(){
return shapeHint;
}
}
现在我已经达到了我开始有点挣扎的 shapeGuesser 类。我不确定如何为我的游戏和它的 JOptionPane 端加入一个守卫。我需要 shapeGuesser 运行,直到用户猜出正确的形状。
我已被指示在开始时向用户展示此选项。
我要问什么问题?
输入数字: 1. 几面?2.你的边长一样吗?3.提示
根据您输入的数字 1、2 或 3。系统将针对该形状询问该问题。因此,您的 Shape 必须准备好适当的响应。
import javax.swing.JOptionPane;
import java.util.Random;
public class shapeGuesser {
public static void main(String[] args, Object Do) {
// TODO Auto-generated method stub
// the shape the program uses
int random;
shape shapeChoice;
// create shapes
square s = new
square(4, "Square", true, "Geeks were called this in the 80s");
Rectangle r = new Rectangle(4, "Rectangle", false, "Not Pentangle");
Triangle t = new Triangle(3, "Triangle",false, "Toblerone");
Circle c = new Circle(0, "Circle",true, "Circle Circle Circle");
//declare shape array
shape[] Shapes;
//create shape array
Shapes = new shape[4];
//put shape objects in shape array
Shapes[0] = s;
Shapes[1] = r;
Shapes[2] = t;
Shapes[3] = c;
// generate random number
random = (int) (1 + (Math.random() * 3));
//pick shape from shape array based on random number
shapeChoice = Shapes[random];
}
}
任何读到这里的人,无论如何都可能有时间启发我。将不胜感激。
谢谢,