在java中使用2D数组时遇到问题
这里比较新,java也比较新。去年春天,我通过了编程1课程,现在我在编程2中,我终于在挣扎。通常我不会寻求帮助,如果你决定帮助,请不要握我的手。我主要是想找个人来帮我完成这项任务。 因此,任务必须:在java中使用2D数组时遇到问题,java,arrays,Java,Arrays,这里比较新,java也比较新。去年春天,我通过了编程1课程,现在我在编程2中,我终于在挣扎。通常我不会寻求帮助,如果你决定帮助,请不要握我的手。我主要是想找个人来帮我完成这项任务。 因此,任务必须: a。声明一个适当的数据结构来保存这样的天空图像 b。将数据文件“sky image.txt”读取到数据结构中 c。一种称为“光源”的方法,用于接收天空图像数据并返回最亮内部光斑的位置作为点。使用Java.awt.Point表示点 d。一种称为“暗源”的方法,它接收天空图像数据,并将最暗的内部光斑的
- a。声明一个适当的数据结构来保存这样的天空图像
- b。将数据文件“sky image.txt”读取到数据结构中
- c。一种称为“光源”的方法,用于接收天空图像数据并返回最亮内部光斑的位置作为点。使用Java.awt.Point表示点
- d。一种称为“暗源”的方法,它接收天空图像数据,并将最暗的内部光斑的位置作为点返回。使用Java.awt.Point表示点
- e。显示天空图像数据的方法
- f。一种称为“filterImage”的方法,用于接收图像数据和整数。然后,该方法将整数添加到所有值中,同时将值保持在0和1024之间。此方法将用于过度曝光(增亮)或曝光不足(变暗)图像。过度曝光是指向所有栅格值添加正值。在“公开”下,向所有栅格值添加负值
- g。一种称为negativeImage的方法,用于生成图像的“负片”。要产生负数,请将任何值x替换为其补码1024–x
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();
}
}
import java.io.*;
导入java.util.*;
公共类Create2DArray{
公共静态int[][]数组;
公共静态int维度1,维度2;
公共静态void main(字符串[]args)引发FileNotFoundException{
File inputFile=新文件(“sky image.txt”);
扫描仪扫描=新扫描仪(输入文件);
维度1=scan.nextInt();
维度2=scan.nextInt();
数组=新整数[dimension1][dimension2];
while(scan.hasNext()){
对于(int row=0;row
在不做所有工作的情况下,这里有一个起点:
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
}
import java.awt.*;
导入java.io.*;
导入java.util.*;
公共课作业{
公共静态void main(字符串[]args)抛出FileNotFoundException{
//完成家庭作业
//读取数据(b)
int[][]数组=加载数据(“sky image.txt”);
//(c)光源
点光源=光源(阵列);
//在这里检查答案,对吗?
//(d)暗源
//…打电话,检查这里是否有e-g。
}
/**b.将数据文件“sky image.txt”读取到数据结构中*/
公共静态int[][]加载数据(字符串文件名){
//你的代码已经在main中,没有重新编写,只是移动了它
文件输入文件=新文件(文件名);
扫描仪扫描=新扫描仪(输入文件);
int dimension1=scan.nextInt();
int dimension2=scan.nextInt();
int[][]数组=新int[dimension1][dimension2];
while(scan.hasNext()){
对于(int row=0;row
好吧,其中一些会告诉你该做什么。例如,“c.一种称为lightsource的方法,它接收天空图像数据并返回…作为一个点”。因此,您需要在类中创建一个方法,如publicstaticjava.awt.pointlightsource(int[][]imageData)
,然后实现它所说的操作,然后对其他每个步骤重复这些说明。你对列表中的哪一部分有问题?我似乎不知道如何把整个事情很好地结合起来,我很难看到过去创建2D数组、初始化并用文件中的数据填充她。完成后,正如您从我的代码中看到的,我不确定如何创建适当的方法来操作数组。看看我在说什么。出于某种原因,我的思维将我的代码转移到使用一个构造函数来创建数组上,比如这个公共Create2Array(int[][]数组,int-dimension1,int-dimension2,p点)抛出FileNotFoundException,然后填充添加我为创建数组准备好的代码基本上我丢失了:0它可能无法编译,我只是在记事本++中键入了它,但应该给您一个起点吗?另外,步骤(a)是“确定适当的数据结构”。在上面的工作中,您的答案是“2d int数组是一种合适的数据结构”。我不会在这里替你回答的