【问题标题】:Composition of void method used in void method? (Java)void方法中使用的void方法的组成? (爪哇)
【发布时间】:2016-09-11 10:39:57
【问题描述】:

我大约 2 周前开始学习 Java,所以请不要评判。 我正在用一个二维数组(一张图片)做这个程序,我想旋转 90 度(已经完成,测试,它可以工作)和 180 度。我的方法是无效的,我想使用 90 度一个两次(作曲?)在 180 度一次,但它不起作用。

这是我的90方法:

public void rotate90(){
        for (int r = 0; r < w; r++) {
             for (int c = 0; c < h; c++) {
                 imageMatrix[c][w-r-1] = imageMatrix[r][c];
             }
        }

public void rotate180(){ 
        rotate90(rotate90()); // my idea was to rotate again the already rotated matrix, but since rotate90 is void it doesn't work
}

有什么办法可以做到这一点吗?使用 void 函数?

提前致谢!

【问题讨论】:

    标签: java arrays for-loop matrix void


    【解决方案1】:

    rotate90() 方法没有参数。其实这不是正确的方法。

    第一种方法是写出来。

    rotate90();
    rotate90();
    

    或使用for-cycle

    for (int i=0; i<2; i++) {
        rotate90();
    }
    

    但是,这里有一种方法可以通过一种方法将其旋转多少次:

    public void rotate90(int n) {
        for (int i=0; i<n; i++) {
            for (int r=0; r<w; r++) {
                for (int c=0; c<h; c++) {
                    imageMatrix[c][w-r-1] = imageMatrix[r][c];
                }
            }
        }
    

    然后是rotate180() 方法:

    public void rotate180(){ 
        rotate90(2); // rotate by 90 two times
    }
    

    【讨论】:

    • 由于某种原因,当我两次调用 rotate90 时它不起作用...你能告诉我更多关于 for-cycle 方式的信息吗?谢谢。
    • 它没有旋转 180,只有 90,我不知道为什么。是否有可能将矩阵旋转 90 度,然后再次旋转相同的矩阵,以旋转新矩阵?我想我只是想另一种方式,而不调用rotate90。再次感谢您的帮助!
    【解决方案2】:

    您只需要调用该方法两次。你不能做的是调用rotate90(),返回值为rotate90,这就是你提议的代码正在做的事情,因为该方法不接受参数或返回值。

    【讨论】:

      【解决方案3】:

      您的rotate90() 直接处理全局变量,所以您的rotate180() 也可以。

      public void rotate180(){ 
          rotate90();
          rotate90();
      }
      

      但是,我建议您使用一些参数和返回值,仅在绝对必要时使用全局变量。另外,我不确定你的算法是否正确,我会这样做。

      public static int[][] rotate90(int[][] matrix){
          int [][] newMatrix = new int[matrix[0].length][matrix.lenght];
      
          for (int r = 0; r < w; r++) {
               for (int c = 0; c < h; c++) {
                   newMatrix[c][w-r-1] = matrix[r][c];
               }
          }
          return newMatrix;
      }
      
      public static int[][] rotate180(){ 
          return rotate90(rotate90()); 
      }
      

      无需将它们设置为static,但由于它们不需要对象即可工作,您可以将它们移动到Utils 类或其他东西。

      【讨论】:

        【解决方案4】:

        如果只想调用一次,可以作为参数传递

        public void rotate90nTimes(int n){
            for (int times = 0; times < n; times++) {
                for (int r = 0; r < w; r++) {
                     for (int c = 0; c < h; c++) {
                         imageMatrix[c][w-r-1] = imageMatrix[r][c];
                     }
                }
            }
        }
        

        附言: 如果您确实想将其用作 rotate90(rotate90),则需要返回矩阵并且不要将函数设为 void。

        【讨论】:

          猜你喜欢
          • 2015-10-03
          • 1970-01-01
          • 1970-01-01
          • 2020-08-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-03
          相关资源
          最近更新 更多