如果值连续增加三次,如何在java中停止循环?

时间:2015-03-15 15:10:33

标签: java loops halt

我是java的新手。我正在写一个摇滚剪刀游戏的样本。用户输入0,1,2分别用于摇滚,纸张和剪刀。程序随机生成结果。我几乎设法让它工作,但我唯一坚持的是如何STOP THE FOR LOOP IF ONE OF THE SIDE WINS CONSECUTIVELY FOR THREE TIMES

这是原始问题

编写一个播放剪刀 - 岩纸游戏的程序。规则是剪刀赢得纸张,a 摇滚胜利剪刀,一张纸赢得一块石头。该程序代表剪刀为0,摇滚 为1,纸为2。 游戏将在计算机和玩家之间进行。该程序将提示用户 输入要赢的轮数。例如,如果用户输入4,则获胜者必须赢得 4轮中至少3轮。如果任何一方连续赢了3次,那么游戏就会提早结束 第四轮。如果用户进入5轮,则获胜者必须在5轮中赢得至少3轮。该 同样的连续3胜规则也适用。用户输入轮数后, 计算机随机生成0到2之间的数字。然后程序提示用户 输入数字0,1或2.在最后一轮之后(根据上述早期获胜者规则), 程序显示一条消息,指示计算机或用户是赢还是输。

这是我的代码。

package assignment1;

import java.util.Scanner;
import java.util.Random;

public class question1_9 {

// This program is used to play scissor-rock-paper game.

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        Random random = new Random();
        int scissor = 0, rock = 1, paper = 2, round, userinput,comprand, userresult = 0, compresult = 0, control,j,k;

        // Variables used in this program is declared and initialized.

        /* Number of rounds wished to be play are obtained from user. */

        System.out.println("WELCOME TO ROCK PAPER SCISSOR GAME. ");
        System.out.println("PLEASE ENTER THE NUMBER OF ROUND YOU WANT TO PLAY:  ");
        round = scan.nextInt();
        control = (round/2)+1;

        for(int i = 0; i<round; i++)
        {
            if (compresult == control | userresult == control)
            {
                break;
            }
            System.out.println("ROUND " + (i+1));
            System.out.println("PLEASE ENTER:\n 0 for scissor \n 1 for rock \n 2 for paper  \n");
            userinput = scan.nextInt();
            comprand = random.nextInt(3);
            if (userinput == 0)
            {
                if (comprand == 0)
                {
                    System.out.println("COMPUTER IS SCISSOR");
                    System.out.println("DRAW!!!");
                    i--;
                }
                else if (comprand == 1)
                {
                    System.out.println("COMPUTER IS ROCK");
                    System.out.println("COMPUTER WINS!!!");
                    compresult++;
                }
                else
                {
                    System.out.println("COMPUTER IS PAPER");
                    System.out.println("YOU WIN!!!");
                    userresult++;
                }
                }
            else if (userinput == 1)
            {
                if (comprand == 0)
                {
                    System.out.println("COMPUTER IS SCISSOR");
                    System.out.println("COMPUTER WINS!!!");
                    compresult++;
                }
                else if (comprand == 1)
                {
                    System.out.println("COMPUTER IS ROCK");
                    System.out.println("YOU WIN!!!");
                    userresult++;
                }
                else
                {
                    System.out.println("COMPUTER IS PAPER");
                    System.out.println("DRAW!!!");
                    i--;
                }
                }
            else
            {
                if (comprand == 0)
                {
                    System.out.println("COMPUTER IS SCISSOR");
                    System.out.println("YOU WIN!!!");
                    userresult++;
                }
                else if (comprand == 1)
                {
                    System.out.println("COMPUTER IS ROCK");
                    System.out.println("DRAW!!!");
                    i--;
                }
                else
                {
                    System.out.println("COMPUTER IS PAPER");
                    System.out.println("COMPUTER WINS!!!");
                    compresult++;
                }
                }
            }
        if(compresult == userresult)
            System.out.println("\n\nFINAL RESULT IS DRAW!!!");
        else if (compresult > userresult)
            System.out.println("\n\nFINAL RESULT IS COMPUTER WIN!!!");
        else
            System.out.println("\n\nFINAL RESULT IS YOU WIN!!!");

        }

    }

2 个答案:

答案 0 :(得分:1)

当您想要退出当前循环时,请使用break;语句。

在循环顶部,

将胜利存储在数组中

String[] results=new String[rounds];

将结果存储为每轮“user”或“comp”,并在循环结束时执行此操作

if((results[i].equals("user") && results[i-1].equals("user") && results[i-2].equals("user") || (results[i].equals("comp") && results[i-1].equals("comp") && results[i-2].equals("comp")))
{
break;
}

答案 1 :(得分:0)

如shreyas所说,当你想退出循环时,请使用break语句。

每当玩家赢得一轮时,我会将compresult设置为零,并在计算机获胜时将userresult设置为零。然后,在循环的顶部,紧跟{添加:

之后
if(userResult == 3 || computerResult == 3 || round/2 > 10)
{
    break;
}

有些代码来自shreyas的原始post