【问题标题】:java method to log the row, column location记录行、列位置的java方法
【发布时间】:2016-08-08 15:18:35
【问题描述】:

我正在做一个在线 JAVA 课程的作业,但我遇到了问题的一部分。毫无疑问,当谈到 JAVA 时,我是一个完全的新手,虽然我在 BASIC 方面做得很好,但我无法理解其中的一些东西。这是我需要做的:

编写一个名为 createCoords 的方法,该方法将搜索 2D 数组以查找可被 3 整除的任何值。找到数字后,您应该记录行、列的位置。这意味着当您的方法完成时,它应该生成一个坐标列表,我可以用它来绘制我的图表。此方法还必须返回可被 3 整除的坐标数,以便我知道要绘制的点数。

只要我得到行、列位置的列表,我并不特别关注坐标是如何返回的。因此,我将留给您制定返回值的机制。一些可能性是:

-字符串

-数组

-2D 数组

我目前只是不确定该怎么做。到目前为止,这是我的代码,我相信我已经将 createCoord 降至它除以三并计数的位置。让我无法理解的是记录的行为:

package lab14;

import java.util.Scanner;
import java.util.Random;

public class Lab14 {

public int[][] create2DArray()
{
    Random r = new Random();
    int[][] array = new int[10][10];
    for(int row = 1; row <= 10; row++)
    {
        for(int col = 1; col <= 10; col++)
        {
            array[row-1][col-1] = r.nextInt(100);
        }
    }
    return array;
}

public int createCoords(int[] array, int x)
        {
            int count = 0;
            for(int i = 0; i < x;i++)
            {
                if(array[i]% 3 == 0)
                {
                    count ++;
                }
            }
            return count;
        }

public void print2DArray(int[][] array)
{
    for(int row = 0; row < 10; row++)
    {
        for(int col = 0; col < 10; col++)
        {
            System.out.print(array[row][col] + "\t");
        }
        System.out.println("");
    }
}

public static void main(String[] args) 
{
    int ar[][];        
    Lab14 c = new Lab14();
    ar = c.create2DArray();
    c.print2DArray(ar);
}    
}

【问题讨论】:

  • 首先不要做 row = 1 然后 row - 1,从 0 开始,该死的......

标签: java arrays logging random methods


【解决方案1】:

此示例生成坐标列表。我试图保留你的代码(虽然格式有点):

import java.util.Random;
import java.util.ArrayList;
import java.util.List;
import java.lang.String;

public class Test {

    public static class Coord {
        public int row;
        public int col;
    }

    public int[][] create2DArray() {
        Random r = new Random();
        int[][] array = new int[10][10];
        for(int row = 0; row < 10; row++) {
            for(int col = 0; col < 10; col++) {
                array[row][col] = r.nextInt(100);
            }
        }
        return array;
    }

    public void print2DArray(int[][] array) {
        for(int row = 0; row < 10; row++) {
            for(int col = 0; col < 10; col++) {
                System.out.print(array[row][col] + "\t");
            }
            System.out.println("");
        }
    }

    public List<Coord> getResult(int[][] array) {
        List<Coord> result = new ArrayList<>();

        for(int row = 0; row < 10; row++) {
            for(int col = 0; col < 10; col++) {
                if (array[row][col] % 3 == 0) {
                    Coord coord = new Coord();
                    coord.row = row;
                    coord.col = col;
                    result.add(coord);
                }
            }
        }
        return result;
    }

    public static void main(String[] args) {
        int ar[][];        
        Test c = new Test();
        ar = c.create2DArray();
        c.print2DArray(ar);

        List<Coord> coords = c.getResult(ar);
        for (Coord coord: coords) {
            System.out.println(String.format("%d, %d", coord.row, coord.col));
        }
    }
}

getResult 生成您的作业要求的列表(数组)。

【讨论】:

    【解决方案2】:

    使用二维数组记录:

    int ar[][];
    int coordsAr[][] = new int [10][2]; // 2 columns to store the row index and the column index of the element in ar[][]  
    
    public static void main(String[] args) 
    {       
       Lab14 c = new Lab14();
       ar = c.create2DArray();
       c.print2DArray(ar);
       int count = c.createCoords(ar);
    }
    
    public int createCoords(int[][] array)
    {
        int count = 0;
        for(int i = 0; i < 10;i++)
        {
            for(int j=0;j<10;j++)
                  if(array[i]% 3 == 0)
                  {
                      coordsAr[count] = {i,j};
                      count ++;
                  }
            }
            return count;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多