回到Java循环的开始?

时间:2015-09-16 16:44:27

标签: java loops for-loop while-loop do-while

我必须为初学者Java课程创建一个基本的三轮石头剪刀游戏。除了我的"再次播放,我已经让我的代码中的所有内容都能正常工作了?"命令。我知道我应该为此使用一个循环,但我并不完全确定在代码中插入循环的位置。这是我目前的代码:

Others

我应该使用哪种类型的循环以及它应放在何处?非常感谢提前!

6 个答案:

答案 0 :(得分:0)

在main方法后面使用while循环。

int playAgain = 1;
while(playAgain == 1){
//your code
System.out.println("Would you like to play again? Enter 1 for yes, 2 for no");
//Get User input here and set it to the variable playAgain
}
}//end main
}//end class

答案 1 :(得分:0)

查看“continue”关键字的文档,特别是使用“标签”。

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html 为“ContinueWithLabelDemo”执行“在页面中查找”。

基本上,您可以标记一行以继续执行。

答案 2 :(得分:0)

boolean playing = true;
while(playing){
    //your game code
System.out.println("Would you like to play again?");
    //get user input
if(user said no){
    playing = false;
 }

这是一个如何通过程序保持连续循环执行的基本概要

答案 3 :(得分:0)

问题在于代码很长且很难维护......快速修复将全部包含在一个函数中(更改' main' to' game')然后在main中添加一个布尔值。这个布尔值将决定是否要运行另一个游戏:

public static void game() { /*all code of main goes here*/ }



public static void main(String[] args) {

boolean quitGame=false;

while(!quitGame){
    game(); // calling to start a game
    System.out.println("Play again?  Enter Yes or No.");
    Scanner keyboard = new Scanner(System.in);
    String input3 = keyboard.next();
    if (input3.equalsIgnoreCase("Yes"))
    {

    }
    else
    {
        quitGame = true; // this will exit the loop 
    }
}

}

答案 4 :(得分:0)

<强>答案:

我重构了你的代码,希望你能更好地理解为什么我把循环放在我所做的地方:

public static void main( String[] args )
{
    Scanner scanner = new Scanner( System.in );
    Random random = new Random();

    System.out.println( "Welcome to Rock Paper Scissors!  Best two out of three!" );
    boolean playAgain = false;

    do
    {
        int playerWins = 0;
        int computerWins = 0;

        for ( int i = 1; i < 4; i++ )
        {
            System.out.println( "Roundnumber: " + i );
            String playerChoice = inputPlayerChoice( scanner );
            int computerChoice = calculateComputerChoice( random );
            RpsResult result = calculateResult( playerChoice, computerChoice );
            boolean isPlayerWin = result.isPlayerWin();
            boolean isTie = result.isTie();

            if ( !isTie )
            {
                if ( isPlayerWin )
                {
                    playerWins++;
                }
                else
                {
                    computerWins++;
                }
            }

            System.out.println( "Player has won " + playerWins + " times and the computer has won " + computerWins + " times." );
        }
        showFinalResult( playerWins, computerWins );
        System.out.println( "Play again?" );
        playAgain = askIfPlayAgain( scanner );
    }
    while ( playAgain );

    System.out.println( "Thank you for playing!" );
    scanner.close();
}

如果您看一下您将看到的主要方法,我会在程序的主要部分周围放置一个do-while循环。这是为了重复整个游戏。我选择一个do-while循环,因为你想至少运行一次游戏。

由于你总是想要打3轮,我还在内部区域添加了一个for循环。这样做的好处是,您不需要编写代码3次 - 因为在所有3轮实际上都是相同的(userInput,计算计算机选择,看看用户或计算机是否赢了这一轮,写下结果控制台上的回合)

在for循环中显示最终结果后,询问玩家是否要重复游戏并设置playAgain变量,如果错误则会打破循环。

有关您的信息:

这就是我打破你的代码以适应show main方法的方法。

这些是获得玩家选择和计算机选择的方法:

private static String inputPlayerChoice( Scanner scanner )
    {
        System.out.println( "Please enter \"Rock\", \"Paper\", or \"Scissors\"." );
        String playerChoice = scanner.next();

        return playerChoice;
    }

    private static int calculateComputerChoice( Random random )
    {
        int computerChoice = random.nextInt( 3 ) + 1;
        return computerChoice;
    }

这就是如何计算一轮的结果 - 结果总是与我将其移动到自己的方法相同:

private static RpsResult calculateResult( String playerInput, int computerChoice )
    {
        boolean playerWin = false;
        boolean tie = false;

        if ( playerInput.equalsIgnoreCase( "Rock" ) )
        {
            switch ( computerChoice )
            {
            case 1:
                System.out.println( "Rock vs Rock, Tie!" );
                tie = true;
                break;
            case 2:
                System.out.println( "Rock vs Paper, Computer Wins!" );
                break;
            case 3:
                System.out.println( "Rock vs Scissors, Player wins!" );
                playerWin = true;
                break;
            }
        }
        else if ( playerInput.equalsIgnoreCase( "Paper" ) )
        {
            switch ( computerChoice )
            {
            case 1:
                System.out.println( "Paper vs Rock, Player wins!" );
                playerWin = true;
                break;
            case 2:
                System.out.println( "Paper vs Paper, Tie!" );
                tie = true;
                break;
            case 3:
                System.out.println( "Paper vs Scissors, Computer wins!" );
                break;
            }
        }
        else if ( playerInput.equalsIgnoreCase( "Scissors" ) )
        {
            switch ( computerChoice )
            {
            case 1:
                System.out.println( "Scissors vs Rock, Computer wins!" );
                break;
            case 2:
                System.out.println( "Scissors vs Paper, Player wins!" );
                playerWin = true;
                break;
            case 3:
                System.out.println( "Scissors vs Scissors, Tie!" );
                tie = true;
                break;
            }
        }

        RpsResult result = new RpsResult();
        result.setPlayerWin( playerWin );
        result.setTie( tie );

        return result;
    }

RpsResult是一个简单的存储对象:

public class RpsResult
{
    private boolean playerWin;
    private boolean tie;

    /**
     * @return the playerWin
     */
    public boolean isPlayerWin()
    {
        return playerWin;
    }

    /**
     * @param playerWin
     *            the playerWin to set
     */
    public void setPlayerWin( boolean playerWin )
    {
        this.playerWin = playerWin;
    }

    /**
     * @return the tie
     */
    public boolean isTie()
    {
        return tie;
    }

    /**
     * @param tie
     *            the tie to set
     */
    public void setTie( boolean tie )
    {
        this.tie = tie;
    }

}

要打印结果消息,我使用了这种方法:

private static void showFinalResult( int playerWins, int computerWins )
    {
        System.out.println( "Final result: playerWins " + playerWins + " computerWins " + computerWins );

        if ( playerWins > computerWins )
        {
            System.out.println( "Player has won" );
        }
        else
        {
            System.out.println( "Computer has won" );
        }
    }

问题是玩家是否想再玩一次:

private static boolean askIfPlayAgain( Scanner scanner )
    {
        String input = scanner.next();
        if ( input.equalsIgnoreCase( "Yes" ) )
        {
            return true;
        }
        else
        {
            return false;
        }
    }

答案 5 :(得分:0)

你可以使用一个循环来循环你想要多次。这里有三种不同的类型:for循环,while循环或do while。但是,我建议使用for循环并在方法中使用布尔运算符来决定何时停止循环