1
I was doing an exercise on vectors, where the goal was to build a program that received a number n
for input that would define the size of the vector, and then receive, also by input, others n
numbers to be allocated in this vector.
Thereafter a method should be used to find the largest number among the latter. But the problem I found is precisely in the way I found to take and allocate these n
numbers on my vector.
After testing my program several times I saw that the method is called, but the loop for
, that was before nor is started. I really do not know what can be.
The code:
import java.util.Scanner;
//bubble sort array
public class EP6_1 {
public static int maiorNumero(int array[]) {
int resposta;
for (int x = 0; x == array.length - 1; x++) {
if (array[x] >= array[x+1]) {
for ( int c = 0; c == array.length - 1; c++ ) { //c é o contador
int temp = array[c];
array[c] = array[c+1];
array[c+1] = temp;
}
}
}
resposta = array[array.length-1];
return resposta ;
}
public static void main(String[] args) {
Scanner leitor = new Scanner(System.in);
int lista[];
System.out.println("Digite o tamanho do vetor"); //numero de elementos
int N = leitor.nextInt(); //da lista
lista = new int[N];
for (int i = 0; i == N - 1; i++) { //i é o contador
System.out.println("teste");
lista[i] = leitor.nextInt();
}
System.out.println(">>>>>>>");
System.out.println(maiorNumero(lista));
}
}
Provide the code in textual form always, so that it is possible not only to view, but also to execute.
– user28595
Before you see the code, and it’s hard to see how your IDE looks (why don’t you glue the code here and make it easy for us?) you get the wrong idea of what a compiler is, and maybe what a program is. It doesn’t show because there is no error that the compiler can detect and that prevents the compilation, but it has one of the infinite errors that a code can have, and it’s part of the programmer’s job to find when the co-conspirator can’t find it for you. With the code I can see if I can say anything else.
– Maniero
Sorry, this is my first post. I just edited and put the code, but I believe that also not got the desired formatting
– Bruno_SMR
Code formatting tips: https://answall.com/editing-help#code
– hkotsubo
No matter where it’s written
i == N - 1
swap fori <= N - 1
– Augusto Vasques
Making an analogy - well simplistic - the compiler is like a "spell checker". You can write a text without grammatical errors - which the broker considers correct - but which only says nonsense (and this the broker does not detect). Just as you can write code without compilation errors (and therefore the compiler does not complain), but with wrong logic (does not do what should).
– hkotsubo
@Bruno_smr Did any of the answers solve your question? Do you think you can accept one of them? See [tour] how to do this, if you haven’t already done so. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (you will get enough score right after you accept an answer).
– Maniero