爪哇 - 二维数组 - 绘制点 [英] Java - Two-Dimensional Arrays - Plotting Points

查看:129
本文介绍了爪哇 - 二维数组 - 绘制点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java类我正在作业。我们正在讨论二维数组,但在这个特别的任务,我无法弄清楚如何恢复回来的特定点,并与特定的值设置这些点。这里是分配:


  

编写一个叫做create2DArray,将填补,创建方法和返回
  一个10×10的二维数组的随机数中的范围为1〜100写
  一个名为print2DArray方法将打印行10×10二维数组
  列的方式。写一个方法叫做createCoords,将搜索
  2D阵列寻找那是由3整除的任何值。
  一旦你发现了一些你应该记录的行,列位置。
  这意味着,当你的方法完成它应该产生的列表
  坐标,我可以用它来绘制图形我。此方法也必须
  返回由3,使得我整除坐标数
  知道有多少点有到情节。我不是特别就如何
  只要坐标都将返回我得到的行的列表,
  列位置。于是,我将它留给你制定出一个机制
  为返回值。要测试你的记录
  坐标创建另一个函数调用fillLocations即会
  填补了数组中已登录的地点
  -1。所以,你的程序应该按照此顺序流程


  
  

      
  1. create2DArray

  2.   
  3. print2DArray

  4.   
  5. createCoords

  6.   
  7. fillLocations

  8.   
  9. print2DArray

  10.   

我的理解,并已完成create2DArray和print2DArray,但我想不通createCoords和fillLocations。以下是我迄今为止,但它不工作,有错误present:

 公众诠释createCoords(INT ROW1,诠释COL1){
INT [] [] = COORDS新INT [ROW1] [COL1];
INT [] [] =计数INT新[0] [0];
INT共同= 0;
对于(INT行= 0;排小于10;排++)
{
    对于(INT COL = 0;西小于10;西++)
    {
    如果(COORDS [行] [COL]%3 == 0)
        合作++;
        返回计数[行] [COL];
    }
}
返回合作;}
公众诠释fillLocations(INT [] []计数){
INT X = 0;
INT Y = 0;
对于(INT行= 0;排小于10;排++)
{
    对于(INT COL = 0;西小于10;西++)
    {
    如果(计数[行] [COL]%3 == 0)
        X =排;
        Y =关口;
        打破;
    }
}
返回(X,Y);}


解决方案

作为一个程序员你几乎总是需要研究做不同的事情。当你把你的问题更小的问题,这项研究将更加容易。

例如,你需要生成随机数?因此,在谷歌搜索这一点,你会发现这样的:<一href=\"http://stackoverflow.com/questions/363681/generating-random-integers-in-a-specific-range\">Generating随机整数在特定范围内的

您需要创建并返回一个二维数组?谷歌和看<一个href=\"http://stackoverflow.com/questions/12231453/syntax-for-creating-a-two-dimensional-array\">Syntax用于创建二维阵列

和与你的艺术,把拼图拼在一起的,让你想要的结果的方法。

 公众诠释[] [] create2DArray(){
    INT [] [] newArray =新INT [10] [10];
    随机随机=新的随机();
    INT范围= 100;
    的for(int i = 0;我小于10;我++)
    {
        对于(INT J = 0; J&LT;常用3 [0]。长度; J ++)
        {
            newArray [I] [j]的=(random.nextInt(范围)+1);
        }
    }
    返回newArray;
 }

这个方法,创建并返回 10 * 10 二维数组1-100之间填充用随机生成的数字。我敢肯定,你可以写你的程序的其余部分,自己从中享受。

I have an assignment for a JAVA class I am taking. We are discussing two-dimensional arrays, however on this particular assignment, I can not figure out how to return back specific points and set those points with a specific value. Here is the assignment:

Write a method called create2DArray that will fill, create, and return a 10 x 10 2d array with random numbers in the range of 1 to 100. Write a method called print2DArray that will print a 10 x 10 2D array in row column fashion. Write a method called createCoords that will search the 2D array looking for any value that is evenly divisible by 3. Once you have found a number you should log the row, column location. This means when your method finishes it should produce a list of coordinates that I can use to plot my graph. This method must also return the number of coordinates that are divisible by 3 so that I know how many points there are to plot. I am not particular as to how the coordinates are returned back as long as I get a list of the row, column locations. So, I will leave it to you to work out a mechanism for returning the values. To test that you have logged the coordinates create another function called fillLocations that will fill the locations in the array you have logged with -1. So, your program should flow in this order

  1. create2DArray
  2. print2DArray
  3. createCoords
  4. fillLocations
  5. print2DArray

I understand and have completed create2DArray and print2DArray, but I can not figure out createCoords and fillLocations. Here is what I have so far, but it does not work and there are errors present:

public int createCoords(int row1, int col1){
int[][] coords = new int[row1][col1];  
int[][] count = new int[0][0];
int co = 0;
for(int row = 0; row < 10; row++)
{
    for(int col = 0; col < 10; col++)
    { 
    if(coords[row][col] % 3 == 0)
        co++;
        return count[row][col];
    }
}
return co;}
public int fillLocations(int[][] count){
int x = 0;
int y = 0;
for(int row = 0; row < 10; row++)
{
    for(int col = 0; col < 10; col++)
    {
    if(count[row][col] % 3 == 0)
        x = row;
        y = col;
        break;
    }
}
return (x, y);}

解决方案

As a programmer you'll nearly always need to research for doing different things. This research will be easier when you divide your problem to smaller problems.

For example you need to generate random numbers? So search on google that and you'll find this: Generating random integers in a specific range.

You need to create and return a 2D array? Google and see Syntax for creating a two-dimensional array

And with your art, put the pieces of the puzzle together in a way that gives your desired result.

public int[][] create2DArray() {        
    int[][] newArray = new int[10][10];
    Random random = new Random();
    int range = 100;
    for(int i = 0; i < 10; i++)
    {
        for(int j = 0;j<arr[0].length;j++)
        {
            newArray[i][j]= (random.nextInt(range) + 1);
        }
    }
    return newArray;
 }

This method, creates and returns a 10*10 2D array filled with random generated numbers between 1-100. I'm sure you can write the rest of your program and enjoy from it by yourself.

这篇关于爪哇 - 二维数组 - 绘制点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆