1
He needed help to put the smaller and larger method to be "heard" in the main method. The logic of the program is to show the user what is the highest and lowest value of a vector.
CODE:
import java.util.Scanner;
public class L6ex3 {
public static void main(String[] args) {
Scanner e = new Scanner (System.in);
int quantidade = 0;
System.out.print("Quantos alunos serão cadastradas as idades?");
quantidade = e.nextInt();
int v[] = new int [quantidade];
System.out.print ("Digite as idades");
for (int i=0; i<quantidade; i++)
v[i] =e.nextInt();
}
public int menorValor (int v[]){
int menor =0;
for (int i=0; i<v.length;i++)
menor = v[i];
for (int i=0; i<v.length; i++)
if (v[i]<menor)
menor =v[i];
return menor;
}
public int maior (int v[]){
int maior=0;
for (int i=0; i<v.length;i++)
if (v[i]> maior)
maior = v[i];
return maior;
}
}
Man, it was worth a lot of help. It was just right. As for your suggestion, I had done it this way, but if the user enters with any number greater than 0 it will return 0, because the smallest starts already worth 0. Hugs.
– Josias Ribeiro
In this case, you can start the variable
menor
with a very high number, for example 10000, because the ideal is that you do not have many loops for unnecessary. And remembering, the best way to thank a good answer is to accept it as "best response" :)– regmoraes
I thought of those hypotheses too, really better to give a crumb and put negative than increase a for.. rsrs.. even "computationally" speaking.. Vlw dude, I’m going in. I approved the answer...
– Josias Ribeiro