在intellij IDEA命令行参数中应该输入什么?我有一个文件input.txt和output.txt(一个矩阵是 从第一个文件中读取,新文件将显示在第二个文件中)应向命令行参数传递什么内容,以及如何在语法上正确传递?
- 我有程序逻辑,它在想法中工作,但我需要通过控制台运行它,这需要命令行参数。*
package vsu.cs.vega;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
Solution output = new Solution();
String error = "You entered a non-rectangular matrix, please check the data and
re-enter.";
try {
output.readMtx();
}
catch (ArrayIndexOutOfBoundsException exception){
System.err.print(error);
}
}
}
package vsu.cs.vega;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.lang.ArrayIndexOutOfBoundsException;
public class Solution {
void readMtx() throws IOException {
BufferedReader br = new BufferedReader(new
FileReader("readInFileOutFromFile/src/vsu/cs/vega/input.txt"));
ArrayList<String> lines = new ArrayList<>();
while (br.ready()) {
lines.add(br.readLine());
}
int matrixHeight = lines.size();
int[][] matrix = new int[matrixHeight][];
for(int i = 0; i < matrixHeight; ++i) {
String[] nums = lines.get(i).split("\s*,\s*");
matrix[i] = new int[nums.length];
for(int j = 0; j < nums.length; ++j) {
matrix[i][j] = Integer.parseInt(nums[j]);
}
}
int[][] matrixForOutput = calc(matrix);
try(PrintWriter out = new PrintWriter(new
FileOutputStream("readInFileOutFromFile/src/vsu/cs/vega/output.txt"))) {
for (int i = 0; i < matrixHeight; ++i) {
out.println(Arrays.toString(matrixForOutput[i]).replaceAll("^\\[|]$",
""));
}
}
catch (ArrayIndexOutOfBoundsException ignored){
}
//out.println(Arrays.toString(matrixForOutput).replaceAll("^\\[|]$", ""));
}
int[][] calc(int[][] array) {
int rows = array.length;
int cols = array[0].length;
boolean[] minRows = null, maxRows = null;
boolean[] minCols = null, maxCols = null;
Integer min = null;
Integer max = null;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (null == min || array[i][j] < min) {
minRows = new boolean[rows];
minRows[i] = true;
minCols = new boolean[cols];
minCols[j] = true;
min = array[i][j];
} else if (array[i][j] == min) {
minRows[i] = true;
minCols[j] = true;
}
if (null == max || array[i][j] > max) {
maxRows = new boolean[rows];
maxRows[i] = true;
maxCols = new boolean[cols];
maxCols[j] = true;
max = array[i][j];
} else if (array[i][j] == max) {
maxRows[i] = true;
maxCols[j] = true;
}
}
}
int rowsToDelete = 0, colsToDelete = 0;
for (int i = 0; i < rows; i++) {
if (minRows[i] || maxRows[i]) {
rowsToDelete++;
}
}
for (int i = 0; i < cols; i++) {
if (minCols[i] || maxCols[i]) {
colsToDelete++;
}
}
if (rows == rowsToDelete || cols == colsToDelete) {
return new int[1][0];
}
int[][] result = new int[rows - rowsToDelete][cols - colsToDelete];
for (int i = 0, r = 0; i < rows; i++) {
if (minRows[i] || maxRows[i])
continue; // пропустить строку, содержащую минимум или максимум
for (int j = 0, c = 0; j < cols; j++) {
if (minCols[j] || maxCols[j])
continue; // пропустить столбец, содержащий минимум или максимум
result[r][c++] = array[i][j];
}
r++;
}
//out.println(Arrays.toString(array).replaceAll("^\\[|]$", ""));
return result;
}
}
1条答案
按热度按时间c8ib6hqw1#
你的程序没有使用main方法的“String[] args”参数。所以你不需要提供“Program arguments”。在IntelliJ的“Run Configurations”中,你可以将输入字段留空,或者输入你想要的内容。这并没有太大的区别。
但是,例如,您可以通过程序参数传递路径名“input.txt”和“output.txt”,而不是在“readMtx”方法中对其进行硬编码。