在java中遇到2D数组问题

时间:2015-09-22 22:55:15

标签: java arrays

这里相当新,对java来说相当新。通过我的编程1课程和A弹簧,现在我在编程2,我终于挣扎了。通常情况下,我不会寻求帮助,如果您决定提供帮助,请不要牵着我的手。我主要是想找一个人来完成这项任务。 所以作业必须:

  • 一个。声明一个适当的数据结构来保存这样的天空图像。
  • 湾将数据文件“sky image.txt”读入数据结构。
  • ℃。一种称为“lightSource”的方法,它接收天空图像数据并返回最亮的内部光点的位置作为一个点。使用Java.awt.Point表示一个点。
  • d。一种称为“darkSource”的方法,它接收天空图像数据并将最暗的内部光点的位置作为一个点返回。使用Java.awt.Point表示一个点。
  • 即一种显示天空图像数据的方法。
  • F。一种名为“filterImage”的方法,用于接收图像数据和整数。然后,该方法将整数添加到所有值,同时将值保持在0和1024之间。此方法将用于过度曝光(变亮)或曝光不足(变暗)图像。过度曝光是指为所有网格值添加正值。向所有网格值添加负值时曝光不足。
  • 克。一种名为negativeImage的方法,用于生成图像的“负片”。要产生负数,请将任何值x替换为其补码1024 - x

现在我遇到的问题是,我创建了2D数组读取文件并填充它,我只是不确定如何继续。我不希望你们中的任何人把答案交给我,或者重点是什么。关于如何进行的任何想法都会很棒,因为我一整天都在思考这些想法,但这些想法似乎都不符合我的意愿

代码:

import java.io.*;
import java.util.*;

public class Create2DArray {
    public static int[][] array;
    public static int dimension1, dimension2;

    public static void main(String[] args) throws FileNotFoundException {
            File inputFile = new File("sky image.txt");
            Scanner scan = new Scanner(inputFile);
            dimension1 = scan.nextInt();
            dimension2 = scan.nextInt();
            array = new int[dimension1][dimension2];
            while (scan.hasNext()) {
                for (int row = 0; row < dimension1; row++) {
                    for (int column = 0; column < dimension2; column++) {
                        array[row][column] = scan.nextInt();
                        System.out.printf("%4d ", array[row][column]);
                    }
                    System.out.println();
                }

            }
            scan.close();
        }


}

1 个答案:

答案 0 :(得分:0)

没有完成所有工作,这是一个起点:

import java.awt.*;
import java.io.*;
import java.util.*;

public class Homework {

    public static void main(String[] args) throws FileNotFoundException {       
        // run through the homework

        // read the data (b)
        int[][] array = loadData("sky image.txt");

        // (c) lightSource
        Point light = lightSource(array);
        // check the answer here, is it right?

        // (d) darkSource

        // ... calls, checks for e-g here.

    }

    /** b. Reads the data file “sky image.txt” into the data structure. */
    public static int[][] loadData(string fileName) {
        // your code that is already in main, didn't re-write it just moved it
        File inputFile = new File(fileName);
        Scanner scan = new Scanner(inputFile);
        int dimension1 = scan.nextInt();
        int dimension2 = scan.nextInt();
        int[][] array = new int[dimension1][dimension2];
        while (scan.hasNext()) {
            for (int row = 0; row < dimension1; row++) {
                for (int column = 0; column < dimension2; column++) {
                    array[row][column] = scan.nextInt();
                    System.out.printf("%4d ", array[row][column]);
                }
                System.out.println();
            }

        }
        sc.close();

        return array;
    }

    /** 
     * c. A method called “lightSource” that receives the sky image data and returns the location of the brightest interior spot as a point. 
     * Use Java.awt.Point to represent a point. 
     */
    public static java.awt.Point lightSource( int[][] imageData ) {
        // write code here
        return new java.awt.Point( 0, 0 );
    }


    // put more methods here
}