[英]Guessing game using JOptionPane - Java
所以我已經學習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];
}
}
讀了這么遠的任何人都可能有時間以任何方式啟發我。 將不勝感激。
謝謝,
isEqual()需要在基類shape中實現,就像您要在所有形狀上調用的所有方法一樣 。 使基本形狀返回false。 (理想情況下,形狀應該是抽象的,因此您不能有基本的形狀對象,只能有正方形,矩形等。但是沒關系,您是新手,沒有其他人會使用它。因此,您自己永遠無法創建基本形狀但是,對於將來,這就是^^的抽象含義。然后,讓您所有其他形狀都像廣場已經做的那樣覆蓋基isEqual()。
你做得好! 您選擇了一個隨機形狀,並創建了許多形狀。
現在創建一個打印選項的循環,
system.out.println("Enter Number: 1.How many sides? 2.Are your sides the same length? 3. Hint");
然后獲取用戶輸入,並將其解析為整數。 使用該整數使用if / else / else或switch / case。 (或者,使用if / else / else並按原樣使用字符串,但是請確保使用.equals()而不是==)
所以現在您問了一個問題,然后選擇了一個。 現在打印
if(userInput.equals("1")){
system.outprintln("How many sides? " + shapeChoice.getSides());
}
對2和3做同樣的事情。您將要處理shapeChoice,因此必須調用shape的基本方法。 但是,在運行時,如果對象是正方形或矩形,則在調用shapeChoice.getSides()時,它將調用正方形或矩形實現,從而為您提供所需的答案! :)
然后,您要做的就是循環播放,一遍又一遍地問這個問題,如果用戶願意,讓他猜測並檢查答案! (比較.equals(shapeChoice.getName()))
因此,有一個很大的while(true)永久循環,您可以在其中循環提問,然后檢查他們是否要回答。 如果他們回答正確,您就會爆發。 否則,您會循環回去,並不斷詢問他們想要的提示。
編輯:實際上,既然我正在研究它,由於您正在練習多態,因此您可能應該多使用它。 現在,您擁有單獨的類,但是在構造它們時要傳遞所有信息。 代替:
square s = new square(4, "Square", true, "Geeks were called this in the 80s");
Rectangle r = new Rectangle(4, "Rectangle", false, "Not Pentangle");
有更像
square s = new square();
並具有Square定義的一部分固有定義
public class square extends shape {
//square member variables
boolean equalSides = true;
int numSides = 4;
//and so on
//OR even better, don't define them, since the base class already does!
//merely set the values in the constructor
public square(){
numSides = 4;
equalSides = true;
shapeHint = "Geeks were called this in the 80s";
}
}
每個方形對象都會采用這種方式,因此沒有理由將其作為參數。 這是正方形定義的一部分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.