java - 在我的游戏中添加一个循环

标签 java loops if-statement while-loop

我有一个运行完美的游戏。我想放一行代码,询问玩家是否想在游戏结束时再次玩游戏。我还想为每个玩家和计算机获胜保留一个评分系统。

我在 input = Integer.parseInt(sc.nextInt()); 行上遇到了问题

import java.util.Scanner;

public class Sticks {

    public static boolean whoStart(String choice) {
        int ran = (int) (Math.random() * 2 + 1);

        String ht = "";
        switch (ran) {
            case 1:
                ht = "head";
                break;
            case 2:
                ht = "tails";
        }
        if (ht.equals(choice.toLowerCase())) {
            System.out.println("you start first");
            return true;
        } else {
            System.out.println("computer starts first");
            return false;
        }
    }

    public static int playerTurn(int numberOfSticks) {
        System.out.println(" \nthere are " + numberOfSticks + " sticks ");
        System.out.println("\nhow many sticks do you wanna take? 1 or 2?");
        Scanner in = new Scanner(System.in);
        int sticksToTake = in.nextInt();
        while ((sticksToTake != 1) && (sticksToTake != 2)) {
            System.out.println("\nyou can only take 1 or 2 sticks");
            System.out.println("\nhow many sticks do you wanna take?");
            sticksToTake = in.nextInt();
        }
        numberOfSticks -= sticksToTake;
        return numberOfSticks;
    }

    public static int computerTurn(int numberOfSticks) {
        int sticksToTake;
        System.out.println("\nthere are " + numberOfSticks + " sticks ");
        if ((numberOfSticks - 2) % 3 == 0 || (numberOfSticks - 2 == 0)) {
            sticksToTake = 1;
            numberOfSticks -= sticksToTake;
        } else {
            sticksToTake = 2;
            numberOfSticks -= sticksToTake;
        }
        System.out.println("\ncomputer took " + sticksToTake + " stick ");
        return numberOfSticks;
    }

    public static boolean checkWinner(int turn, int numberOfSticks) {

         int score = 0;
         int input;
         int B = 1;
         int Y=5, N=10;

        if ((turn == 1) && (numberOfSticks <= 0)) {
            System.out.println("player lost");
            return true;
        }
        if ((turn == 2) && (numberOfSticks <= 0)) {
            System.out.println("player won");
            score++;
            return true;
        }

        System.out.println("Your score is "+ score);
        System.out.println("Do you want to play again? Press (5) for Yes / (10) for No");

        // ----- This line -----
        input = Integer.parseInt(sc.nextInt());

        if (input == Y) {
          B = 1;
          System.out.println("Rock, Paper, Scissors");
        } else if (input == N) {
            System.exit(0);
            System.out.println("Have A Good Day!");
        }
    }

    public static void main(String args[]) {
        int turn;
        int numberOfSticks = 21;
        Scanner in = new Scanner(System.in);
        System.out.println("choose head or tails to see who starts first");
        String choice = in.next();
        if (whoStart(choice) == true) {
            do {
                turn = 1;
                numberOfSticks = playerTurn(numberOfSticks);
                if (checkWinner(turn, numberOfSticks) == true) {
                    break;
                };

                turn = 2;
                numberOfSticks = computerTurn(numberOfSticks);
                checkWinner(turn, numberOfSticks);
            } while (numberOfSticks > 0);
        } else {
            do {
                turn = 2;
                numberOfSticks = computerTurn(numberOfSticks);
                if (checkWinner(turn, numberOfSticks) == true) {
                    break;
                };
                turn = 1;
                numberOfSticks = playerTurn(numberOfSticks);
                checkWinner(turn, numberOfSticks);
            } while (numberOfSticks > 0);
        }
    }
}

最佳答案

您的问题的标题几乎回答了您需要添加的内容:一个循环!

为了可读性,我建议您重构函数 main 并从中提取所有游戏逻辑以存储在专用函数中。我们称它为 startGame()

您的 main 将变得更短,并且可以代表一个很好的位置来引入此循环,例如:

public static void main(String[] a) {
    boolean isPlaying = true;
    Scanner in = new Scanner(System.in);

    while(isPlaying) {
        startGame();

        // Your message to continue or stop the game
        if(in.next().equalsIgnoreCase("No")) {
            isPlaying = false;
        }
    }
}

我建议您使用在 while 循环中检查的 boolean 而不是使用 break 语句,因为它带来了更好的效果控制应用程序中的流程。

关于java - 在我的游戏中添加一个循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42731112/

相关文章:

c++ - 具有相同返回的多个 IF

java - 我如何循环这个决策结构?

java - 访问集合中的数据

javascript - jquery 中的 for 循环 <p> 标签

python - 使用字典替换数据框中的字符串值

java - 如何在一个长的for循环中清空所有变量,避免程序内存不足?

excel - 在 true 子句中使用 If-Then 语句的结果

java - 无法让我的 jsp 存储结果集中的整数值

java - java客户端/服务器应用程序没有输出

java - EditText、OnTouchListener 和 setSelection 在第一次触摸时不起作用