Java 我可以对for循环中的计数器进行例外处理吗?
我正试图制作一个程序,用电脑玩石头、布、剪刀。然而,任务的一部分是让玩家选择玩电脑的回合数。如果打成平局,这一轮就不算比赛了 我的代码是这样的——我更关注程序的Java 我可以对for循环中的计数器进行例外处理吗?,java,if-statement,for-loop,counter,Java,If Statement,For Loop,Counter,我正试图制作一个程序,用电脑玩石头、布、剪刀。然而,任务的一部分是让玩家选择玩电脑的回合数。如果打成平局,这一轮就不算比赛了 我的代码是这样的——我更关注程序的部分: int gamePlays = 0; for (int = 0; i < gamePlays; i++) { // If, else if, and else statements to make the game possible. // Here is an example of part of the code
部分:
int gamePlays = 0;
for (int = 0; i < gamePlays; i++)
{
// If, else if, and else statements to make the game possible.
// Here is an example of part of the code:
if (cpuChoice.equals ("rock"))
{
if (userChoice.equals ("scissors"))
{
System.out.println("Rock beats scissors, computer wins.");
}
else if (userChoice.equals ("paper"))
{
System.out.println("Paper beats rock, computer wins.");
}
else
{
System.out.println("You both chose rock! It's a tie.");
}
// The rest would else if cpuChoice.equals "paper" and else.
}
int gamePlays=0;
对于(int=0;i
如果最后一个else
语句是柜台上的平局,我该如何排除它?我已经纠结了几个小时,找不到任何解决办法。我建议而不是for
while
int i = 0;
while (i < gamePlays)
{
// If, else if, and else statements to make the game possible.
// Here is an example of part of the code:
if (cpuChoice.equals ("rock"))
{
if (userChoice.equals ("scissors"))
{
System.out.println("Rock beats scissors, computer wins.");
i++; // increment only when there is result
}
else if (userChoice.equals ("paper"))
{
System.out.println("Paper beats rock, computer wins.");
i++; // increment only when there is result
}
else
{
System.out.println("You both chose rock! It's a tie.");
// don't increment if no result
}
// The rest would else if cpuChoice.equals "paper" and else.
}
inti=0;
而(我<游戏性)
{
//If、else If和else语句使游戏成为可能。
//以下是部分代码的示例:
如果(cpuChoice.equals(“岩石”))
{
if(userChoice.equals(“剪刀”))
{
System.out.println(“石头战胜剪刀,电脑赢”);
i++;//仅当有结果时才递增
}
else if(userChoice.equals(“纸张”))
{
System.out.println(“纸胜石头,电脑胜。”);
i++;//仅当有结果时才递增
}
其他的
{
System.out.println(“你们俩都选择了摇滚!这是一个平局。”);
//如果没有结果,不要增加
}
//如果cpuChoice.equals“paper”和else,则其余的将是else。
}
}我建议用而不是来代替
int i = 0;
while (i < gamePlays)
{
// If, else if, and else statements to make the game possible.
// Here is an example of part of the code:
if (cpuChoice.equals ("rock"))
{
if (userChoice.equals ("scissors"))
{
System.out.println("Rock beats scissors, computer wins.");
i++; // increment only when there is result
}
else if (userChoice.equals ("paper"))
{
System.out.println("Paper beats rock, computer wins.");
i++; // increment only when there is result
}
else
{
System.out.println("You both chose rock! It's a tie.");
// don't increment if no result
}
// The rest would else if cpuChoice.equals "paper" and else.
}
inti=0;
而(我<游戏性)
{
//If、else If和else语句使游戏成为可能。
//以下是部分代码的示例:
如果(cpuChoice.equals(“岩石”))
{
if(userChoice.equals(“剪刀”))
{
System.out.println(“石头战胜剪刀,电脑赢”);
i++;//仅当有结果时才递增
}
else if(userChoice.equals(“纸张”))
{
System.out.println(“纸胜石头,电脑胜。”);
i++;//仅当有结果时才递增
}
其他的
{
System.out.println(“你们俩都选择了摇滚!这是一个平局。”);
//如果没有结果,不要增加
}
//如果cpuChoice.equals“paper”和else,则其余的将是else。
}
}如果发生这种情况,只需将
i--;
那就不包括这一轮了。
谢谢你的提问
for (int = 0; i < gamePlays; i++)
{
// If, else if, and else statements to make the game possible.
// Here is an example of part of the code:
if (cpuChoice.equals ("rock"))
{
if (userChoice.equals ("scissors"))
{
System.out.println("Rock beats scissors, computer wins.");
}
else if (userChoice.equals ("paper"))
{
System.out.println("Paper beats rock, computer wins.");
}
else
{
System.out.println("You both chose rock! It's a tie.");
i--;
}
// The rest would else if cpuChoice.equals "paper" and else.
}
for(int=0;i
这将使“i”保持不变,因为在循环之后,您的“i”将递增。如果发生这种情况,只需将
i--;
那就不包括这一轮了。
谢谢你的提问
for (int = 0; i < gamePlays; i++)
{
// If, else if, and else statements to make the game possible.
// Here is an example of part of the code:
if (cpuChoice.equals ("rock"))
{
if (userChoice.equals ("scissors"))
{
System.out.println("Rock beats scissors, computer wins.");
}
else if (userChoice.equals ("paper"))
{
System.out.println("Paper beats rock, computer wins.");
}
else
{
System.out.println("You both chose rock! It's a tie.");
i--;
}
// The rest would else if cpuChoice.equals "paper" and else.
}
for(int=0;i
这将使“i”保持不变,因为在循环之后,您的“i”将递增。您的for
循环创建一个变量i
,您可以在for
循环的主体中修改该变量。如果您希望该代码在用户与CPU连接时不递增计数器,则递减i
您需要它的地方:
int gamePlays = 0
for (int i = 0; i < gamePlays; i++) {
...
else {
// this is where you handle ties
System.out.println("You tied with CPU");
i--; // decrement the counter
}
...
}
int gamePlays=0
对于(int i=0;i
您的for
循环创建了一个变量i
,您可以在for
循环的主体中修改该变量。如果您希望该代码在用户与CPU连接时不增加计数器的效果,请在需要时减小i
:
int gamePlays = 0
for (int i = 0; i < gamePlays; i++) {
...
else {
// this is where you handle ties
System.out.println("You tied with CPU");
i--; // decrement the counter
}
...
}
int gamePlays=0
对于(int i=0;i
首先,您的消息是错误的。当cpuChoice.equals(“rock”)
和userChoice.equals(“paper”)
时,正确的消息是:
纸胜石头,玩家获胜
此外,如果gamePlays=0
,则不会有游戏
因为您可能也想保留分数,所以只需使用分数:
int cpuWins = 0;
int userWins = 0;
while (cpuWins + userWins < gamePlays) {
if (cpuChoice.equals("rock")) {
if (userChoice.equals("scissors")) {
System.out.println("Rock beats scissors, computer wins.");
cpuWins++;
} else if (userChoice.equals("paper")) {
System.out.println("Paper beats rock, player wins.");
userWins++;
} else {
System.out.println("You both chose rock! It's a tie.");
}
} else ...
...
}
}
if (cpuWins > userWins)
System.out.println("Computer is champion.");
else if (cpuWins < userWins)
System.out.println("Player is champion.");
else
System.out.println("There is no champion.");
int-cpuWins=0;
int userWins=0;
同时(cpuWins+userWins