硬币翻转程序中的胜利出错

时间:2017-03-30 08:14:01

标签: java

由于某种原因,我的程序没有正确打印你正确的百分比。我不知道我是否错了,但我不知所措。如果有人能指出我正确的方向,那就太棒了。它是一个简单的程序,抛出一个硬币,用户猜测它的头部或尾部,然后告诉他们是否赢了。我遇到的问题是计算用户想继续玩的次数。

public static void main(String[] args) throws InterruptedException {
        // This program asks the user to predict a toss of a coin.

        Scanner in = new Scanner(System.in);
        boolean done = false;
        String continuePlay = "";
        float count = 0;
        float wins =0;
        do{
        System.out.printf("Do you predict heads or tails for the coin toss: ");
        String prediction = in.next();


        System.out.print("Tossing the coin");
        TimeUnit.SECONDS.sleep(1);
        System.out.print(".");
        TimeUnit.SECONDS.sleep(1);
        System.out.print(".");
        TimeUnit.SECONDS.sleep(1);
        System.out.print(".");
        TimeUnit.SECONDS.sleep(1);
        System.out.println(".");
        TimeUnit.SECONDS.sleep(1);


        Random rand = new Random();
        int toss = Math.abs(rand.nextInt()) % 2;
        if(toss == 0){
            System.out.print("It came up heads.");

        }else{
            System.out.print("It came up tails.");

        }
        if(prediction.equals("h") && toss==0){
            System.out.print(" You win.");
            wins ++;
        }else if(prediction.equals("h") && toss !=0){
            System.out.print(" You loose.");
        }else if(prediction.equals("t") && toss==1){
            System.out.print(" You win.");
            wins ++;
        }else if(prediction.equals("t") && toss!=1){
            System.out.print(" You loose.");
        }


        // Ask user if they want to continue to play
        System.out.print("\nDo you want to play again? (\"c\" to continue \"q\" to quit): ");
        continuePlay = in.next();
        if (continuePlay.equals("c")){
            done = false;
        } else done = true;

        count ++;



        }while(done == false);


        double percentWon = (wins / count) * 100;

        System.out.printf("Thanks for playing. You guessed my number %.0f%% of the time.\n", percentWon);
            in.close();

        }

    }

1 个答案:

答案 0 :(得分:0)

错误可能在你的胜利增量中...我认为每次获胜时,你都会将胜利重置为1 ......在这里

 }
        if(prediction.equals("h") && toss==0){
            System.out.print(" You win.");
            wins =1;
        }else if(prediction.equals("h") && toss !=0){
            System.out.print(" You loose.");
        }else if(prediction.equals("t") && toss==1){
            System.out.print(" You win.");
            wins=1;
        }else if(prediction.equals("t") && toss!=1){
            System.out.print(" You loose.");
        }

尝试改为......

 }
        if(prediction.equals("h") && toss==0){
            System.out.print(" You win.");
            wins ++;
        }else if(prediction.equals("h") && toss !=0){
            System.out.print(" You loose.");
        }else if(prediction.equals("t") && toss==1){
            System.out.print(" You win.");
            wins++;
        }else if(prediction.equals("t") && toss!=1){
            System.out.print(" You loose.");
        }

并取胜利++;最后。