我正在尝试显示最低和最高值。最高值是正确的,但最小值总是给我0你能帮助我吗。
package openlink.domain;
import java.io.*;
public class table2 {
public static void main(String[] args) {
int table1[][] = new int[5][5];
int table2[][] = new int[5][5];
int table3[][] = new int[5][5];
int i, j, k, l, b, c, g, p, num = 0, num1 = 0, small = 0, largest = 0;
String input = " ";
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
table1[i][j] = 0;
}
}
for (k = 0; k < 5; k++) {
for (l = 0; l < 5; l++) {
table2[k][l] = 0;
}
}
try {
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
System.out.print("Input table1[" + i + "][" + j + "]= ");
input = in.readLine();
num = Integer.parseInt(input);
table1[i][j] = num;
}
}
for (k = 0; k < 5; k++) {
for (l = 0; l < 5; l++) {
System.out.print("Input table2[" + k + "][" + l + "]= ");
input = in.readLine();
num1 = Integer.parseInt(input);
table2[k][l] = num1;
}
}
for (b = 0; b < table3.length; b++) {
for (c = 0; c < table3.length; c++) {
table3[b][c] = table2[b][c] + table1[b][c];
}
}
System.out.println("table1");
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
System.out.print(table1[i][j] + " ");
}
System.out.println();
}
System.out.println();
System.out.println("table2");
for (k = 0; k < 5; k++) {
for (l = 0; l < 5; l++) {
System.out.print(table2[k][l] + " ");
}
System.out.println();
}
System.out.println();
System.out.println("table3");
/* this is the code in how to locate the lowest and the highest */
for (b = 0; b < 5; b++) {
for (c = 0; c < 5; c++) {
System.out.print(table3[b][c] + " ");
}
System.out.println();
}
System.out.println();
for (b = 0; b < table1.length; b++) {
for (c = 0; c < table2.length; c++) {
if (small > table3[b][c]) {
small = table3[b][c];
} else if (table3[b][c] > largest) {
largest = table3[b][c];
}
}
}
System.out.println("the highest value in table3: " + largest);
System.out.println("the lowest value in table3: " + small);
} catch (IOException e) {
}
}
}
请在这里帮助我
答案 0 :(得分:6)
好吧,你已经将small
初始化为0开始...所以如果有负数,你只会更新它。同样,您已将large
初始化为0,因此只有在正数字时才会更新。输入所有负数,您会发现small
是正确的,而large
是错误的。
最好先将small
和大table3[0][0]
初始化,然后知道它们都是数组中的有效值。
答案 1 :(得分:0)
问题在于
if (small > table3[b][c]) {
small = table3[b][c];
} else if (table3[b][c] > largest) {
largest = table3[b][c];
}
因为small初始化为0,所以它永远不会大于table3 [b] [c] 尝试使用大量的小的初始化,它应该工作得很好。
旁注:您的代码需要进行一些清理。你的变量需要更多的名称,你不必在顶部为每个for循环声明每个变量 尝试替换:
for (k = 0; k < 5; k++) {
for (l = 0; l < 5; l++) {
System.out.print("Input table2[" + k + "][" + l + "]= ");
input = in.readLine();
num1 = Integer.parseInt(input);
table2[k][l] = num1;
}
}
与
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
System.out.print("Input table2[" + i + "][" + j + "]= ");
input = in.readLine();
num1 = Integer.parseInt(input);
table2[i][j] = num1;
}
}
您也可以通过执行以下操作将其提取到方法中:
private int[][] getFilledArray(int size, String arrayName) {
int[][] myArray = new int[size][size];
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String input = new String();
try {
for (int i = 0; i < myArray.length; i++) {
for (int j = 0; j < myArray[i].length; j++) {
System.out.print("Input " + arrayName + "[" + i + "][" + j + "]= ");
input = in.readLine();
myArray[i][j] = Integer.parseInt(input);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return myArray;
}