所以我一直在学习Java大约8周,并且我不得不设计一个形状猜测游戏。是的,它是家庭作业。所以我用下面的一个例子构建了我的四个形状类。使用JOptionPane进行猜测游戏 - Java
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];
}
}
任何人读到这里,可能有时间来启发我。这将非常感激。
感谢,
提示:您需要某种循环和布尔值来确定游戏是否应该继续。 –
我知道我需要使用While循环,但我甚至不知道从哪里开始。我从来没有必须使用JOptionPane。 – Anderscc
我会先尝试让你的游戏在命令行上工作。在你所有的逻辑中放一段时间,让用户猜测。一旦你有这个工作,你应该把它放到一个GUI中。这里有一个教程:http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html –