ですから、私はJavaを約8週間勉強していて、クラスでは形を推測するゲームを設計する必要がありました。はい、宿題です。そこで、以下の例を使用して4つの形状クラスを作成しました。
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];
}
}
ここまで読んで、とにかく私を啓発する時間があるかもしれない人は誰でも。よろしくお願いします。
ありがとう、