I am still testing, this time I have tried to create a program that locates the largest number in a table.
The program is raised with functions and the table is filled randomly, this is the code:
package recursividad;
import java.util.*;
public class maxTabla {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int max;
int t[];
t = new int[6];
max = max(t);
System.out.println("------------------------------------------------------");
System.out.println("Programa que localiza el numero mayor en una tabla");
System.out.println("------------------------------------------------------");
for (int i = 0; i < t.length; i++) {
t[i] = (int) (Math.random() * 100 + 1);
}
System.out.println("La tabla es: ");
for (int i = 0; i < t.length; i++) {
System.out.println(t[i]);
}
System.out.println("El numero mas grande es: " + max);
}
public static int max(int t[]) {
int max;
max = t[0];
for (int i = 0; i < t.length; i++) {
if (t[i] > max) {
max = t[i];
}
}
return max;
}
}
The problem is that it always tells me that the maximum value is "0"
Thanks in advance, greetings.
The problem is that you execute the function before filling the table. Put the function call
max=max(t);
after filling the table with numbers.The problem you have is very simple. It always tells you that the maximum value is 0 because you are calling the function
max()
before you have any numbers in the table.Put that call after filling the table:
Output: