Checkstyle错误:控制变量'j'被修改。 [英] Checkstyle error : control variable 'j' is modified.

查看:123
本文介绍了Checkstyle错误:控制变量'j'被修改。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能让我的代码更好?



我尝试过:



How could I make my code better?

What I have tried:

<pre>public static int moderatorChoice(int guess, String[] doors) {
    int moderator = 0;
    int i = 1;

    if ((doors[guess - 1].equals("Z"))) {
      for (int j = 0; j < doors.length; j++) {
        if (doors[guess - 1] != doors[j]) {
          while (!(doors[j].equals("A"))) {
            j++;
            moderator = j;
            Terminal.printLine(moderator );
            return j;
          }
        }
      }

      return moderator;

    }

推荐答案

你不应该真正修改你的里面的j 循环;它是外部 for 循环的控制变量:

You shouldn't really be modifying j inside your while loop; it's the control variable for the outer for loop:
for (int j = 0; j < doors.length; j++) {
  if (...) {
    while (...) {
      j++;
      ...
      return j;
    }
  }

你的循环只会执行一次。而是试试这个:

And your while loop will only ever execute once. Instead try this:

for (int j = 0; j < doors.length; j++) {
  if (...) {
    if (...) {
      ...
      return j + 1;
    }
  }

或者更好地将两个条件合并为一个 if

Or better combine the two conditions into a single if


这篇关于Checkstyle错误:控制变量'j'被修改。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆