我们正在创建一个程序,该程序将返回地形图并向用户显示在不进行陡峭爬升或下降的情况下导航地形的最佳方法。对于正在使用的文件,我不确定需要返回什么最大值和最小值。除有关预期返回值的3个错误外,赋值大部分完成。我已经提交了我们正在使用的主要代码,并包含了教授为作业分配给我们的其他文件的链接。
我尝试过返回maxValue,返回minValue,返回max,返回min和其他一些组合,但是问题是我不确定是否应该从此MapDataDrawer.java返回值文件或教授必须用于分配作业的其他两个文件之一。
//MapDataDrawer.java
//This is the code that is returning the error
import java.util.*;
import java.io.*;
import java.awt.*;
public class MapDataDrawer
{
private int[][] grid;
public MapDataDrawer(String filename, int rows, int cols){
// initialize grid
grid = new int[rows][cols];
//read the data from the file into the grid
File dataFile = new File(filename);
try {
Scanner dataInput = new Scanner(dataFile);
for (int i=0; i<rows; i++) {
for (int j=0; j<cols;j++) {
grid[i][j] = dataInput.nextInt();
}
}
} catch (Exception e) { e.printStackTrace();}
}
/**
* @return the min value in the entire grid
*/
public int findMin() {
// Implement this method
}
/**
* @return the max value in the entire grid
*/
public int findMax(){
// Implement this method
}
/**
* @param col the column of the grid to check
* @return the index of the row with the lowest value in the given col for the grid
*/
public int indexOfMinRow(int col){
//Implement this method
}
/**
* Draws the grid using the given Graphics object.
* Colors should be grayscale values 0-255, scaled based on min/max values in grid
*/
public void drawMap(Graphics g){
int min = findMin();
int max = findMax();
for (int i=0; i<480; i++) {
for (int j=0; j<480; j++) {
int c = (255 * (grid[i][j] - min)) / (max - min);
g.setColor(new Color(c, c, c));
g.fillRect(j, i, 1, 1);
}
}
}
/**
* Find a path from West-to-East starting at given row.
* Choose a foward step out of 3 possible forward locations, using greedy method described in assignment.
* @return the total change in elevation traveled from West-to-East
*/
public int drawLowestElevPath(Graphics g, int row){
int elevChange = 0;
// Implement this method
return elevChange;
}
private int minOfThree(int a, int b, int c) {
if ((a > b) && (a > c)) return a;
if ((b > a) && (b > c)) return b;
if ((c > a) && (c > b)) return c;
return 0;
}
}
这些是教授提交的文件,我不确定是否应该从这些文件之一或实际的作业文件(MapDataDrawer.java)https://drive.google.com/drive/folders/1siRoY1K0ngptE2rL-wscXLK8Ct7Qo1hb?usp=sharing
中返回一些值。它还包括我们应该使用的地形数据。
答案 0 :(得分:0)
填写grid
后:
public int findMin() {
int min = Integer.MAX_VALUE;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
int value = grid[i][j];
if (value < min) {
min = value;
}
}
}
return min;
}