繁体   English   中英

使用JOptionPane进行猜游戏-Java

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM