Methods are not executed in the for (if-else) loop and returns me the original object

问题: I have an array of moves to be made a to a rubiks cube i.e.: String[] clockwiseMoves = new String[6]; whereby the moves are denoted as F(front), R(right), U(up), L(lef...

问题:

I have an array of moves to be made a to a rubiks cube i.e.:

String[] clockwiseMoves = new String[6];

whereby the moves are denoted as F(front), R(right), U(up), L(left), B(back), D(down) (e.g. when F is executed, you are to rotated the front face of the cube clockwise). Given an instance of Rubik (from Rubik class) is instantiated, I want it to execute every move in the my clockwiseMoves array.

In my Rubik class, I have these methods:

  1. public Rubik frontfaceRight(): which rotates the front face of the Rubik to the right and return a new instance of Rubik which has been rotated.

  2. public Rubik rightfaceRight(): which rotates the right face of the Rubik to the right and return a new instance of Rubik which has been rotated.

  3. public Rubik upfaceRight(): which rotates the up face of the Rubik to the right and return a new instance of Rubik which has been rotated.

  4. public Rubik leftfaceRight(): which rotates the left face of the Rubik to the right and return a new instance of Rubik which has been rotated.

  5. public Rubik backfaceRight(): which rotates the back face of the Rubik to the right and return a new instance of Rubik which has been rotated.

  6. public Rubik downfaceRight(): which rotates the down face of the Rubik to the right and return a new instance of Rubik which has been rotated.

Given these, I have come up with the following:

for (int j = 0; j<clockwiseMoves.length; j++){

      if (cwMethods[j].equals("F")){
        originalRubik.frontfaceRight();
      }

      else if (cwMethods[j].equals("R")){
        originalRubik.rightfaceRight();
      }

      else if (cwMethods[j].equals("U")){
        originalRubik.upfaceRight();
      }

      else if (cwMethods[j].equals("L")){
        originalRubik.leftfaceRight();
      }

      else if (cwMethods[j].equals("B")){
        originalRubik.backfaceRight();
      }

      else if (cwMethods[j].equals("D")){
        originalRubik.downfaceRight();
      }
    }

System.out.println(originalRubik);

However, my printed output gives me the intial Rubik before it went through the for loop, meaning that the methods in the for loop were not executed. May I know how to correct my code?


回答1:

You mentioned that those methods return a new instance of Rubik, so when you process the actions in the if-else blocks you are not assigning the new instance to the originalRubik variable and it still points to the initial instance of Rubik.

So you need to capture the new instance of the Rubik and assign it to the originalRubik varaible to see the changes.

for (int j = 0; j<clockwiseMoves.length; j++){

  if (cwMethods[j].equals("F")){
    originalRubik = originalRubik.frontfaceRight();
  }

  else if (cwMethods[j].equals("R")){
    originalRubik = originalRubik.rightfaceRight();
  }

  else if (cwMethods[j].equals("U")){
    originalRubik = originalRubik.upfaceRight();
  }

  else if (cwMethods[j].equals("L")){
    originalRubik = originalRubik.leftfaceRight();
  }

  else if (cwMethods[j].equals("B")){
    originalRubik = originalRubik.backfaceRight();
  }

  else if (cwMethods[j].equals("D")){
    originalRubik = originalRubik.downfaceRight();
  }
}

System.out.println(originalRubik);
  • 发表于 2019-03-19 01:51
  • 阅读 ( 170 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除