-1

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

这是最初的问题

编写一个玩剪刀石头布游戏的程序。规则是剪刀战胜纸,石头战胜剪刀,纸战胜石头。程序将剪刀表示为 0,将石头表示为 1,将纸表示为 2。游戏是在计算机和玩家之间进行的。该程序将提示用户输入获胜的回合数。例如,如果用户输入 4,那么获胜者必须在 4 轮中至少赢得 3 轮。如果任何一方连续获胜3次,则比赛提前结束,没有第四轮。如果用户进入 5 个回合,获胜者必须在 5 个回合中至少赢得 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!!!");

        }

    }
4

2 回答 2

1

break;当你想退出当前循环时使用语句。

在循环顶部,

将胜利存储在数组中

String[] results=new String[rounds];

将您的结果存储为每一轮的“用户”或“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;
}
于 2015-03-15T15:16:13.230 回答
0

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

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

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

一些代码来自 shreyas 的原始帖子

于 2015-03-15T16:58:55.937 回答