import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.ArrayList;
public class CastleDefense {
// This is a global variable because both the public CastleDefense and main are using it
double [][] grid;
public CastleDefense(int numRows, int numCols, Double[] scan) {
int k = 0;
grid = new double[numRows][numCols];
//loop that goes across the grid
for (int r = 0; r<numRows; r++){
if (numRows%2==0){
//stores the position of the grid, where we are. (k) ex. second row, we are on the first row and place
for (int g = 0; g<numCols; g++){
grid [r][g] = scan[k];
k++;
}
}
else{
for (int j = numCols; j>=0;j--){
grid [r][j] = scan[k];
k++;
}
}
}
}
public void findWeakest(){
}
public double getAverage(int startRow, int startCols, int endRow, int endCol){
double average = 0.0;
return average;
}
public static void main(String[] args) throws FileNotFoundException {
int rows = 0;
int cols = 0;
Scanner input = new Scanner(new File("data.in"));
//Making the array list to hold the doules in it
ArrayList<Double> arr = new ArrayList<>();
//Switching my aray list to a regular array to pass it to the constructor
Double[] scan = new Double[arr.size()];
//loop to enter the doubles in the list
while (input.hasNextDouble()) {
arr.add(input.nextDouble());
//System.out.println(arr.get(arr.size() - 1));
}
//loop to calculate the number of rows and columns
int i = (int) Math.sqrt(scan.length);
while (i>0){
if (scan.length % i == 0){
rows = i;
cols = scan.length/i;
}
else
i--;
}
//passing the values to the constructor
CastleDefense pyramid = new CastleDefense (rows,cols, scan);
}
}
这是我到目前为止的代码。我的目标是让程序从数字列表中读取(在data.in中)将其存储在一个数组中,然后制作一个网格(最接近最佳平方,因此3x4而非1x12)将这些数字从数组插入网格在第一行从左到右阅读,然后在第二行从右到左阅读,依此类推第四行。然后从网格中取出第一个2x2并添加找到平均值的所有数字,并输出最弱的2x2和平均值。我目前仍然坚持从网格中获取数字的平均值,并找到最弱的数字。