简体   繁体   English

使用Java对象和方法制作Coinflip游戏?

[英]Making a coinflip game using objects and methods java?

I'm trying to make a coin flip game in java. 我正在尝试用Java制作投币游戏。 I'm relatively new to the language and the only other language I knew was Javascript, so I'm still learning. 我是该语言的新手,而我所知道的唯一其他语言是Javascript,因此我仍在学习。 I've already made one before using just one class and putting all the code inside, but I'm now trying to do it with methods since I'm trying to learn them. 在只使用一个类并将所有代码放入内部之前,我已经做过一个了,但是由于要学习它们,所以我现在尝试使用方法来实现。

The basic idea of the game is that the user picks how many coins they'd like to flip, they pick heads or tails, then the computer flips the coins and calculates if there was more heads or tails to tell the user if they won or not. 游戏的基本思想是,用户选择要翻转的硬币数量,选择正面或反面,然后计算机翻转硬币并计算是否有更多的正面或反面,以告知用户是否获胜或失败。不。

I'm not quite complete with the program as you can see but I've already run into an error. 如您所见,我对该程序并不十分完整,但是我已经遇到了错误。 I was just doing some tests on the code I already have and I found that when I call the method settingUpCoin in my main class, the program terminates. 我只是对已有的代码进行一些测试,发现当我在主类中调用方法settingUpCoin时,程序将终止。

So basically, when I run it, it executes userImp right, transform right, but then it doesn't let you enter a value for howManyCoins and terminates before you get to settingUpCoin . 所以基本上,当我运行它,它执行userImp权,权转换,但后来它不会让你输入一个值howManyCoins并终止你之前settingUpCoin Please help! 请帮忙! Thank you! 谢谢! (Also, sorry if I said something really stupid in that explanation I'm really new to programming in general so please humor me :D) (也很抱歉,如果我在解释中说了些愚蠢的话,那么我通常对编程还是陌生的,所以请幽默我:D)

By the way, if you have any other suggestions or tips to help me please feel free to give them just don't reveal too much because I want to see how much I can figure out on my own. 顺便说一句,如果您有任何其他建议或提示可以帮助我,请随时给他们提供一些建议,只是不要透露太多,因为我想看看自己能找到多少。 Once again, thanks for all the help because I know this is a huge messy post that you all are spending your own free time to read! 再次感谢您的所有帮助,因为我知道这是一篇非常凌乱的文章,大家都在浪费自己的空闲时间阅读!

(Notes: this was all done in Eclipse Luna build or the Java IDE. The class files are all separate in the actual thing, I just put them together here to demonstrate my code.) (注意:这都是在Eclipse Luna构建或Java IDE中完成的。类文件在实际情况中都是独立的,我只是将它们放在一起以演示代码。)

Coin.java: Coin.java:

import java.io.IOException;

public class Coin {
    double myCoin;
    int numOfCoins;
    int counter;
    double arrayOfCoins[] = {};

    public void howManyCoins() throws IOException {
        System.out.println("How many coins would you like to flip?");
        numOfCoins = System.in.read();
    }

    public void settingUpCoin() {
        for (counter = 0; counter == numOfCoins; counter++) {
            arrayOfCoins[counter] = Math.floor(Math.random() * 2);
            System.out.println("Adding a coin. Coin number" + counter);
            //each time loop goes consider calling userChoice() method 
        }
    }
}

UserChoice.java: UserChoice.java:

import java.io.IOException;

public class UserChoice {
    char userPick;
    int finalUserPick;

    public void userImp() throws IOException {
        System.out.println("Pick H for heads and T for tails. Make sure to capitalize."); // times number of coins?
        userPick = (char) System.in.read(); // times number of coins?
    }

    public void transform() {
        if (Character.toString(userPick).matches("H")) {
            finalUserPick = 0;
            System.out.println("You picked Heads");
        } else {
            finalUserPick = 1;
            System.out.println("You picked Tails");
        }
    }
}

PutItAllTogether.java: PutItAllTogether.java:

import java.io.IOException;

public class PutItAllTogether {
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        Coin myCoin1 = new Coin();
        UserChoice request = new UserChoice();

        request.userImp();
        request.transform();

        myCoin1.howManyCoins();
        myCoin1.settingUpCoin();
    }
}

Try using this for getting the number of coins: 尝试使用此方法获取硬币数量:

System.out.println("How many coins would you like to flip?");    
BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
String s = br.readLine();
numOfCoins = Integer.parseInt(br.readLine());

Note that you will have to import java.io.BufferedReader. 请注意,您将必须导入java.io.BufferedReader。

Another thing I should mention is that the way you have set up your array won't work. 我还应该提到的另一件事是,设置阵列的方法将行不通。 You initialized it as an empty array and then you try to assign values to it in the for loop. 您将其初始化为空数组,然后尝试在for循环中为其分配值。 It might be easier to do away with the array altogether and just use a counter in the for loop to see how many coins (either heads or tails) you get based on what the user chose. 完全消除该数组可能更容易,而只需在for循环中使用计数器即可根据用户选择的内容查看获得的硬币数量(正面或反面)。 If you're really set on using arrays, you can establish a max number of coins to flip (ex: 10), then create an array of size 10 and fill in the values that way. 如果您确实想使用数组,则可以建立最大数量的硬币进行翻转(例如10),然后创建大小为10的数组并以这种方式填写值。

Finally, you should check out Random's nextInt() function for an easy way to simulate a coin flip. 最后,您应该查看Random的nextInt()函数,以一种简单的方式模拟硬币翻转。

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

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