Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java方法记录行、列位置_Java_Arrays_Logging_Random_Methods - Fatal编程技术网

java方法记录行、列位置

java方法记录行、列位置,java,arrays,logging,random,methods,Java,Arrays,Logging,Random,Methods,我正在为一个在线JAVA类做作业,我被一个问题的一部分卡住了。无可否认,我对JAVA是一个完全的新手,尽管我在BASIC方面做得很好,但我还是无法理解其中的一些东西。以下是我需要做的: 编写一个名为createCoords的方法,该方法将搜索2D数组,查找任何可被3整除的值。找到号码后,应记录行、列和位置。这意味着,当您的方法完成时,它将生成一个坐标列表,我可以使用该列表绘制图形。此方法还必须返回可被3整除的坐标数,以便我知道有多少点要绘制 只要我得到一个行、列位置的列表,我就不知道如何返回坐标

我正在为一个在线JAVA类做作业,我被一个问题的一部分卡住了。无可否认,我对JAVA是一个完全的新手,尽管我在BASIC方面做得很好,但我还是无法理解其中的一些东西。以下是我需要做的:

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

只要我得到一个行、列位置的列表,我就不知道如何返回坐标。因此,我将留给您来制定一种返回值的机制。有些可能性是:

-串

-排列

-二维阵列

我现在只是不确定该怎么办。这是到目前为止我的代码,我相信我已经创建了createCoord,它除以3并进行计数。我没有意识到的是记录日志的行为:

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);
}    
}
lab14包装;
导入java.util.Scanner;
导入java.util.Random;
公共类Lab14{
public int[]create2DArray()
{
随机r=新随机();
int[][]数组=新的int[10][10];
对于(int row=1;row使用二维数组记录:

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;
}
int-ar[];
int-coordsAr[][]=new int[10][2];//2列,用于在ar[][]中存储元素的行索引和列索引
公共静态void main(字符串[]args)
{       
Lab14 c=新的Lab14();
ar=c.create2DArray();
c、 打印阵列(ar);
int count=c.createCoords(ar);
}
公共int-createCoords(int[][]数组)
{
整数计数=0;
对于(int i=0;i<10;i++)
{
对于(int j=0;j使用2D数组记录:

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;
}
int-ar[];
int-coordsAr[][]=new int[10][2];//2列,用于在ar[][]中存储元素的行索引和列索引
公共静态void main(字符串[]args)
{       
Lab14 c=新的Lab14();
ar=c.create2DArray();
c、 打印阵列(ar);
int count=c.createCoords(ar);
}
公共int-createCoords(int[][]数组)
{
整数计数=0;
对于(int i=0;i<10;i++)
{

对于(int j=0;j,此示例生成一个坐标列表。我已尝试保留您的代码(尽管有点格式化):

import java.util.Random;
导入java.util.ArrayList;
导入java.util.List;
导入java.lang.String;
公开课考试{
公共静态类坐标{
公共int row;
公共int col;
}
public int[]create2DArray(){
随机r=新随机();
int[][]数组=新的int[10][10];
对于(int行=0;行<10;行++){
for(int col=0;col<10;col++){
数组[行][col]=r.nextInt(100);
}
}
返回数组;
}
public void print2DArray(int[][]数组){
对于(int行=0;行<10;行++){
for(int col=0;col<10;col++){
System.out.print(数组[行][col]+“\t”);
}
System.out.println(“”);
}
}
公共列表getResult(int[][]数组){
列表结果=新建ArrayList();
对于(int行=0;行<10;行++){
for(int col=0;col<10;col++){
if(数组[行][col]%3==0){
坐标坐标=新坐标();
coord.row=行;
coord.col=col;
结果。添加(协调);
}
}
}
返回结果;
}
公共静态void main(字符串[]args){
int-ar[][];
测试c=新测试();
ar=c.create2DArray();
c、 打印阵列(ar);
列表坐标=c.getResult(ar);
对于(协调人:协调人){
System.out.println(String.format(“%d,%d”,coord.row,coord.col));
}
}
}

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

此示例生成坐标列表。我已尝试保留您的代码(尽管格式有点):

import java.util.Random;
导入java.util.ArrayList;
导入java.util.List;
导入java.lang.String;
公开课考试{
公共静态类坐标{
公共int row;
公共int col;
}
public int[]create2DArray(){
随机r=新随机();
int[][]数组=新的int[10][10];
对于(int行=0;行<10;行++){
for(int col=0;col<10;col++){
数组[行][col]=r.nextInt(100);
}
}
返回数组;
}
public void print2DArray(int[][]数组){
对于(int行=0;行<10;行++){
for(int col=0;col<10;col++){
System.out.print(数组[行][col]+“\t”);
}
System.out.println(“”);
}
}
公共列表getResult(int[][]数组){
列表结果=新建ArrayList();
对于(int行=0;行<10;行++){
for(int col=0;col<10;col++){
if(数组[行][col]%3==0){
坐标坐标=新坐标();
coord.row=行;
coord.col=col;
结果。添加(协调);
}
}
}
返回结果;
}
公共静态void main(字符串[]args){
int-ar[][];
测试c=新测试();
ar=c.create2DArray();
c、 打印阵列(ar);
列表坐标=c.getResult(ar);
对于(协调人:协调人){
System.out.println(String.format(“%d,%d”,coord.row,coord.col));
}
}
}

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

首先不要执行row=1,然后是row-1,从0开始,该死……首先不要执行row=