为我的游戏添加循环

时间:2017-03-11 03:48:38

标签: 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);
        }
    }
}

2 个答案:

答案 0 :(得分:2)

您问题的标题几乎已经回答了您需要添加的内容:循环

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

你的主要变得更短,并且可以代表引入这个循环的好位置,例如:

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;
        }
    }
}

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

答案 1 :(得分:1)

只需将所有内容放入while(true)循环中,如果选择“否”,则使用break;。类似的东西:

static int playerPoints = 0;

public static void main(String args[]) {
    int turn;
    int numberOfSticks = 21;
    Scanner in = new Scanner(System.in);
    while(true){

        ...

        System.out.println("You have " + playerPoints + " points!")
        System.out.println("Do you want to play again?");
        if (!in.nextLine().toLowerCase().equals("yes")){
            break;
        }
    }
}

编辑:ZenLulz的答案比这个更好,主要是因为它鼓励更好的编程实践。我的工作但不是解决问题的最佳方法。