我可以在for循环中对计数器进行异常处理吗?

时间:2016-05-25 03:41:22

标签: java if-statement for-loop counter

我试图制作一个用电脑玩摇滚,纸,剪刀的程序。但是,部分任务是让玩家选择与计算机一起玩的轮数。如果有平局,则该轮不应计入比赛。

我的代码看起来像这样 - 我更关注程序的for部分:

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. 
}

如果从计数器获得平局,我如何排除最后else声明?我已经和我搏斗了几个小时,无法找到任何解决方案。

4 个答案:

答案 0 :(得分:3)

而不是08:23:22,041 [-] [Siddhi-R_H_Match-executor-thread-1] INFO TenantId : -1234, Event Processor : R_H_Match, Event Stream : matchingStream:1.0.0 (ms), after processing _[Event{timestamp=1464144801945, data=[27, phone, g1, sell, samsung, galaxy note, type, 16, phone, g1, buy, samsung, galaxy note, type], isExpired=false}] (Sanitized) 08:23:22,120 [-] [Siddhi-R_H_Match-executor-thread-1] INFO TenantId : -1234, Event Processor : R_H_Match, Event Stream : matchingStream:1.0.0 (ms), after processing _[Event{timestamp=1464144802037, data=[27, phone, g1, sell, samsung, galaxy note, type, 16, phone, g1, buy, samsung, galaxy note, type], isExpired=false}] (Sanitized) 我建议@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); SetIP setip = new SetIP(); setip.hello(); }

for

}

答案 1 :(得分:2)

您的for循环会创建一个变量i,您可以在for循环的主体中修改该变量。如果您希望代码在用户与CPU绑定时具有 not 递增计数器的效果,则在您需要的位置递减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
   }
   ...
}

答案 2 :(得分:1)

如果发生这种情况,请填写

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. 
}

这将使&#39; i&#39;保持不变,因为你的“我”是将在循环后递增。

答案 3 :(得分:0)

首先,你的消息是错误的。当cpuChoice.equals("rock")userChoice.equals("paper")时,正确的消息是:

  

Paper beats rock, player 获胜。

此外,如果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.");