在继续问题上做

时间:2018-11-06 20:07:44

标签: java

因此,我使用input.nextLine();解决了延续问题。但是,当我运行代码时,它将运行两次迭代,然后退出程序。我注意到当代码再次完全重复时,它会执行此操作。我的代码现在看起来像这样:

package morrisJCh4Sec8to9;

import java.util.Scanner;

public class CoinToss {
    public static void main (String args[]) {
        Scanner input = new Scanner (System.in);
        String call = " ";
        String heads = "h";
        String tails = "t";
        String play = " ";
        String playAgain = "c";
        double numWins = 0;
        double totPlay = 0;
        if(call == null || call.length() > 1) {
            System.exit(1);
        }

        int toss = (int)(Math.random() * 2);
        do {
            System.out.println("Please call the coin toss (h or t): "); 
            //this line keeps being repeated 
            call = input.nextLine();
            System.out.println("Tossing...");
            //this line shows up before it is supposed to
            if(heads.equals(call)) { //checks if the call is equal to heads
                System.out.println("The coin came up heads. You win!");
                numWins = numWins + 1; //keeps track of the wins
                totPlay = totPlay + 1; //keeps track of total times played

            }
            else if(call.equals(tails)) { //if call is tails
                System.out.println("The coin came up heads. Sorry you 
                lose!");
                totPlay = totPlay + 1;
            }

            else if(tails.equals(call)) {
            //if call is tails
                System.out.println("The coin came up tails. You win!");
                numWins = numWins + 1;
                totPlay = totPlay + 1;
                }
            else {
                System.out.println("The coin came up tails. Sorry you 
                lose!"); // call was heads
                totPlay = totPlay + 1;
            }   


                System.out.println("Would you like to play again? (type c to 
                continue or any other letter to quit)");

                play = input.next();
                if(play.compareTo(playAgain) != 0) {
                    System.out.println("Thanks for playing!");
                    break;
                }
                else { //goes through the iteration again
                    System.out.println("Please call the coin toss (h or 
                    t)"); 
                    input.nextLine();

                    call = input.nextLine();
                    System.out.println("Tossing...");
                   if(heads.equals(call)) { //checks if the call is equal to heads
                System.out.println("The coin came up heads. You win!");
                numWins = numWins + 1; //keeps track of the wins
                totPlay = totPlay + 1; //keeps track of total times played

            }
            else if(call.equals(tails)) { //if call is tails
                System.out.println("The coin came up heads. Sorry you 
                lose!");
                totPlay = totPlay + 1;
            }

            else if(tails.equals(call)) {
            //if call is tails
                System.out.println("The coin came up tails. You win!");
                numWins = numWins + 1;
                totPlay = totPlay + 1;
                }
            else {
                System.out.println("The coin came up tails. Sorry you 
                lose!"); // call was heads
                totPlay = totPlay + 1;
            }   


                System.out.println("Would you like to play again? (type c to 
                continue or any other letter to quit)");

                play = input.next();
                if(play.compareTo(playAgain) != 0) {
                    System.out.println("Thanks for playing!");
                    break;
                }
                    continue; //after two times of iteration, the code exits
                }

        }
        while(toss < 1);

        System.out.println("Great game! You were correct " + (int) ((numWins 
        * 100 ) / totPlay) + "% of the time."); //prints the number of times 
        //user was correct.
        input.close();
    }
}

输出看起来像这样: 请调用抛硬币(h或t): H 折腾... 硬币冒出来了。你赢了! 您想再玩一次吗? (输入c继续或退出其他任何字母) C 请调用抛硬币(h或t): H 折腾... 硬币冒出来了。你赢了! 您想再玩一次吗? (输入c继续或退出其他任何字母) C 精彩的比赛!您100%的时间都是正确的。

0 个答案:

没有答案