在掷硬币程序中获胜的错误



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

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......在这里

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

并拿赢++;最后出来。

最新更新