私は約8週間Javaを勉強していましたが、クラスのために形状の推測ゲームを設計しなければなりませんでした。はい、それは宿題です。そこで私は4つのシェイプクラスを以下の例で構築しました。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);
}
}
私は、それは私が、私はほんの少し苦労し始めていますshapeGuesserクラスに達したことを、今だ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;
}
}
を作成しました。私は自分のゲームとその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 –