简体   繁体   English

使用JOptionPane进行猜游戏-Java

[英]Guessing game using JOptionPane - Java

So I have been studying Java for about 8 weeks and for class I had to design a shape guessing game. 所以我已经学习Java大约8周了,在上课时,我不得不设计一个形状猜测游戏。 Yes it is Homework. 是的,这是家庭作业。 So I have built my four shape classes with an example of one below. 因此,我用下面的一个示例构建了四个形状类。

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);
    }
}

I then created a shape.java class 然后,我创建了一个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;
    }
}

It's now that I have reached the shapeGuesser class that I am starting to struggle just a little. 现在,我已经到达了shapeGuesser类,我开始为此感到有些挣扎。 I'm unsure how to incorporate a guard for my game and the JOptionPane side of it. 我不确定如何在我的游戏及其JOptionPane方面加入后卫。 I need shapeGuesser to run until the user guesses the correct shape. 我需要shapeGuesser运行,直到用户猜测正确的形状为止。

I have been instructed to present the user with this option at the start. 指示我在开始时向用户提供此选项。

What question shall I ask? 我问什么问题?

Enter Number: 1.How many sides? 输入数字:1.多少面? 2.Are your sides the same length? 2.你的边长一样吗? 3. Hint 3.提示

Based on the number you enter 1, 2 or 3.That question will be asked of that shape. 根据您输入的数字1、2或3,将询问该形状的问题。 So your Shape must have an appropriate response ready. 因此,您的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];               

    }

}

Anyone who read this far and might have the time to enlighten me in anyway. 读了这么远的任何人都可能有时间以任何方式启发我。 It would be much appreciated. 将不胜感激。

Thanks, 谢谢,

isEqual() needs an implementation in the base class, shape, as with all methods you want to call on all your shapes. isEqual()需要在基类shape中实现,就像您要在所有形状上调用的所有方法一样 Have the base shape return false. 使基本形状返回false。 (Ideally shape should be abstract so you can't have a basic shape object, only squares, rectangles, etc. but its okay, you're new, and nobody else will use this. So you yourself can just never create a base shape. But for the future, that's what abstract is for ^^) Then, have all your other shapes override that base isEqual() the way your square already does. (理想情况下,形状应该是抽象的,因此您不能有基本的形状对象,只能有正方形,矩形等。但是没关系,您是新手,没有其他人会使用它。因此,您自己永远无法创建基本形状但是,对于将来,这就是^^的抽象含义。然后,让您所有其他形状都像广场已经做的那样覆盖基isEqual()。

You're doing good! 你做得好! You've selected a random shape, and created many shapes. 您选择了一个随机形状,并创建了许多形状。

Now create a loop that prints the options, 现在创建一个打印选项的循环,

system.out.println("Enter Number: 1.How many sides? 2.Are your sides the same length? 3. Hint");

Then take the user input, and parse it to an integer. 然后获取用户输入,并将其解析为整数。 Have an if/else/else or a switch/case using that integer. 使用该整数使用if / else / else或switch / case。 (alternatively, use if/else/else with the string as it is, but make sure to use .equals() not ==) (或者,使用if / else / else并按原样使用字符串,但是请确保使用.equals()而不是==)

So now you've asked a question, and selected one. 所以现在您问了一个问题,然后选择了一个。 Now you print out 现在打印

if(userInput.equals("1")){
system.outprintln("How many sides? " + shapeChoice.getSides());
}

Do the same thing for 2 and 3. You'll be dealing with a shapeChoice, so you have to call the base methods of shape. 对2和3做同样的事情。您将要处理shapeChoice,因此必须调用shape的基本方法。 However, at runtime, if the object is a square or a rectangle, when you call shapeChoice.getSides() it will invoke the square or rectangle implementation, giving you the answer you want! 但是,在运行时,如果对象是正方形或矩形,则在调用shapeChoice.getSides()时,它将调用正方形或矩形实现,从而为您提供所需的答案! :) :)

Then all you have to do is loop back, ask the question over and over again, and if the user wants to, let him guess, and check his answer! 然后,您要做的就是循环播放,一遍又一遍地问这个问题,如果用户愿意,让他猜测并检查答案! (compare .equals(shapeChoice.getName())) (比较.equals(shapeChoice.getName()))

so have a big while(true) forever loop, inside of which you can ask a question, and then check if they want to answer. 因此,有一个很大的while(true)永久循环,您可以在其中循环提问,然后检查他们是否要回答。 If they answer right, you break out. 如果他们回答正确,您就会爆发。 Otherwise, you loop back around, and keep asking them which hint they'd like. 否则,您会循环回去,并不断询问他们想要的提示。

EDIT: Actually, now that I look at it, since you're practicing polymorphism, you should probably use it a little more. 编辑:实际上,既然我正在研究它,由于您正在练习多态,因此您可能应该多使用它。 Right now, You have your separate classes, but you're passing in all your information when you construct them. 现在,您拥有单独的类,但是在构造它们时要传递所有信息。 Instead of: 代替:

square s = new square(4, "Square", true, "Geeks were called this in the 80s");
Rectangle r = new Rectangle(4, "Rectangle", false, "Not Pentangle");

Have it be more like 有更像

square s = new square();

and have part of square's definition inherently define 并具有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";
    }
} 

EVERY square object is going to be this way, so there's no reason it should be a parameter. 每个方形对象都会采用这种方式,因此没有理由将其作为参数。 That is part of the definition of a square. 这是正方形定义的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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