Print in descending order in Java

Asked

Viewed 641 times

1

I’m trying to create an interface where notes (decimals) are typed, and printed in descending order, but I’m getting some errors that I don’t quite understand.

This is the class for the user to type in the number of students x, and thus typing x notes.

import java.util.Arrays;
import javax.swing.JOptionPane;

public class Numeros{
    public static void main(String args[]) {
        int numAlunos = Integer.parseInt(JOptionPane.showInputDialog("Forneca a qtde de alunos"));
        float notas[] = new float[numAlunos]; 

        for (int i = 0; i < numAlunos; i++){
            notas[i] = Float.parseFloat(JOptionPane.showInputDialog("Forneca nota do aluno"));
        }
        for (int i = 0; i < numAlunos; i++){
            System.out.println("Nota: "+ notas[i]);     

        }
    }
}

This is the class that would organize the decreasing grades:

public class App{
    public static void main(String args[]) {
        Numeros nm = new Numeros();
        for (int i = 1; i < notas.length; i++); {
            for( j = 0; j < i; j++); {
                if (notas[i] > notas[j]); {
                    int temp = notas[i];
                    notas[i] = notas[j];
                    notas[j] = temp;
                }
            }
        }
    }
}

The first works correctly, the second says it can’t find the variables notas, i and j.

1 answer

0

The method main is the "input point" of a Java program, is where the program starts running.

In case, you created a main in each class, which makes them "independent programs": each can perform without having knowledge of the other.

In addition, the variable notas raised inside Numeros.main is not visible to the class App. Any variable created within a method is local to that method (contrary to another answer stated (before being erased), notas nay is a class attribute Numeros, rather a local method variable main). That’s why the class App cannot find the variable notas.

In addition, within App.main, create an instance using new Numeros() nay executes the main class Numeros. In fact you only created one instance of the class Numeros, but he didn’t use it for anything.


One way to solve it is to get the class Numeros return the array of notes that have been read. Thus, in the class App you get this array and sort:

public class Numeros {
    public float[] lerNotas() {
        int numAlunos = Integer.parseInt(JOptionPane.showInputDialog("Forneca a qtde de alunos"));
        float notas[] = new float[numAlunos];

        for (int i = 0; i < numAlunos; i++) {
            notas[i] = Float.parseFloat(JOptionPane.showInputDialog("Forneca nota do aluno"));
        }
        for (int i = 0; i < numAlunos; i++) {
            System.out.println("Nota: " + notas[i]);
        }
        return notas; // retorna o array de notas
    }
}

public class App {
    public static void main(String args[]) {
        Numeros nm = new Numeros();
        float[] notas = nm.lerNotas(); // obter as notas
        // faz a ordenação
        for (int i = 1; i < notas.length; i++) {
            for (int j = 0; j < i; j++) {
                if (notas[i] > notas[j]) {
                    float temp = notas[i];
                    notas[i] = notas[j];
                    notas[j] = temp;
                }
            }
        }
        System.out.println("Notas ordenadas:");
        for (int i = 0; i < notas.length; i++) {
            System.out.println("Nota: " + notas[i]);
        }
    }
}

Another detail is that there were some ; the more. When you do this:

for (int i = 1; i < notas.length; i++);
{  etc... }

Are you saying that inside the for has nothing (or has an "empty block"), because the ; indicates that there is nothing inside this for. And what comes next (the block { etc... } nay will be part of the for.

That’s why the program says it can’t find the i nor the j. They only exist within the respective for in which they were declared, but as the for ended in the ;, the following block cannot see these variables (since it is not part of the for).

The same goes for the ; shortly after the if (notas[i] > notas[j]), right after the parentheses is not to have semicolon, otherwise the block that comes next will not be part of the if. Anyway, remove these ; hence.


Another alternative is to leave the method lerNotas static. As it does not depend on any specific class state Numeros, there is no reason to create an instance just to use the method:

public class Numeros {
    // mudei o método para static
    public static float[] lerNotas() {
        // resto do código é igual ao anterior
    }
}
public class App {
    public static void main(String args[]) {
        // como lerNotas é static, não precisa criar uma instância de Numeros
        float[] notas = Numeros.lerNotas();
        // resto do código é igual ao anterior
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.