2016-04-16 4 views
1

オンラインJAVAクラスの割り当てに取り組んでおり、問題の一部に固執してしまいました。私は基本的にはうまくいったが、JAVAについて言えば、まったく初心者である。私はこのようなものの周りを頭で覆うことはできない。ここで私は何をする必要があるかです:行、列の場所をログに記録するJavaメソッド

は、2D配列はあなたが行をログに記録すべき番号を見つけたら3で割り切れる任意の値を探して検索しますcreateCoordsと呼ばれる方法で、列の位置を書きます。つまり、メソッドが終了すると、グラフをプロットするために使用できる座標のリストが生成されるはずです。このメソッドはまた、3で割り切れる座標の数を返さなければならないので、プロットする点の数を知ることができます。

私は行、列の場所のリストを取得する限り、座標が返される方法については特にありません。だから、私は値を返すための仕組みを作るためにそれをあなたに任せます。いくつかの可能性があります:

-array

-2D配列

を - ストリング

は、私は現在、何をすべきかのちょうどわかりませんよ。これまでのところ私のコードは、私は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); 
}  
} 
+0

まず最初に、行= 1と行-1を実行しないでください。ちょうど0の神のことで始まります。 – ScriptKiddy

答えて

0

この例では、座標のリストを生成します。私は(ビットフォーマットされたが)あなたのコードを維持しようとしている:

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は、あなたの割り当てがを求めていることリスト(配列)を生成します。

0

は、ログインするために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; 
} 
関連する問題