如果一个值连续递增三次,如何停止java中的循环?
我是java新手。我正在写一个石头布剪刀游戏的样本。用户分别为石头、布、剪刀输入0、1、2。程序随机生成结果。我几乎成功了,但唯一让我困扰的是,如果一方连续三次获胜,如何如果一个值连续递增三次,如何停止java中的循环?,java,loops,halt,Java,Loops,Halt,我是java新手。我正在写一个石头布剪刀游戏的样本。用户分别为石头、布、剪刀输入0、1、2。程序随机生成结果。我几乎成功了,但唯一让我困扰的是,如果一方连续三次获胜,如何停止FOR循环 这是原来的问题 写一个程序,玩剪刀岩纸牌游戏。规则是剪刀赢了一张纸,一把剪刀赢了一张纸 石头战胜剪刀,纸张战胜石头。该程序将剪刀表示为0,rock 作为1,纸张作为2。 这个游戏是在计算机和玩家之间进行的。程序将提示用户 输入要赢的回合数。例如,如果用户输入4,则获胜者必须在 4轮中至少有3轮。如果任何一方连续赢
停止FOR循环
这是原来的问题
写一个程序,玩剪刀岩纸牌游戏。规则是剪刀赢了一张纸,一把剪刀赢了一张纸
石头战胜剪刀,纸张战胜石头。该程序将剪刀表示为0,rock
作为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!!!");
}
}
包分配1;
导入java.util.Scanner;
导入java.util.Random;
公开课问题1_9{
//这个程序是用来玩剪刀岩纸牌游戏的。
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
随机=新随机();
int scissor=0,rock=1,paper=2,round,userinput,comprand,userresult=0,compresult=0,control,j,k;
//声明并初始化此程序中使用的变量。
/*从用户处获得希望播放的回合数*/
System.out.println(“欢迎来到摇滚剪纸游戏”);
System.out.println(“请输入您想要玩的回合数:”;
round=scan.nextInt();
控制=(轮/2)+1;
for(int i=0;i userresult)
System.out.println(“\n\n最终结果是COMPUTER WIN!!!”;
其他的
System.out.println(“\n\n最终结果是您赢了!!!”;
}
}
使用中断要退出当前循环时的代码>语句
在循环的顶部
将胜利存储在一个数组中
String[] results=new String[rounds];
将每轮的结果存储为“用户”或“比较”,并在循环结束时执行此操作
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;
}
正如shreyas所说,当您想要退出循环时,请使用break
语句
每当玩家赢了一轮,我就会将compresult
设置为零,每当计算机赢了,我就会将userresult
设置为零。然后,在循环的顶部,就在{
添加之后:
if(userResult == 3 || computerResult == 3 || round/2 > 10)
{
break;
}
有些代码来自shreyas的原始代码。并非所有情况下都是如此。假设有10场比赛,我赢了第2、4、6场比赛,你的解决方案将导致循环在不连续时达到收支平衡。我理解。我编辑了我的答案,请尝试一下。你的解决方案将牺牲另一个获胜条件。如果双方都没有得到三个s怎么办你能赢并且一直玩到最后吗?