Check equal numbers java

Asked

Viewed 771 times

0

Guys, can someone help me out here.

I did a check of 3 notes inside a while.

I need that if 2 notes are equal a message Aluno1 and Aluno2 (Show only if the two typed notes are equal) Approved. I managed to do only with the highest note.

package calculo;

import java.util.Scanner;

public class Calculo {

    public static void main(String[] args) {
        float maior, num;
        int count = 2;

     Scanner entrada = new Scanner(System.in);

     System.out.print("Nota Aluno 1: ");
     num = entrada.nextFloat();
     maior = num;

     while(count <= 3){
      System.out.print("Nota Aluno " + count + ": ");
      num = entrada.nextFloat();

      if(num > maior){
       maior = num;
      }

      count++;
     }

     System.out.println("O maior numero digitado é: " + maior);

    }
    }
  • 1

    if(num == maior){ System.out.println("Aluno 1 e Aluno2")} ?

  • I tried this way but I did not succeed, like this problem runs like this, I type 3 notes, if I put 3, 4 and 5 the highest grade is approved that would be the 5, if I type 3 5 and 5 the two notes are approved. In the code it looks like this ("Student Note " + Count + ": ") it speaks which student is. If student 1 and student 2 are the same, it appears "Approved"

2 answers

1

An auxiliary class aluno will help a lot in this case:

public class Aluno{

    private int id;
    private float nota;

    public Aluno(int id,float nota){
        this.id = id;
        this.nota = nota;
    }

    public int getId() {
        return id;
    }

    public float getNota() {
        return nota;
    }
}

And the main would be simplified to something like this:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class notasIguais{
    
    public static void main(String[] args) {
        List<Aluno> alunos = new ArrayList<>();
        Scanner reader = new Scanner(System.in);

        int numero_de_alunos = 3; //Numero de alunos que pretende dar input
        int count = 0;

        while(count < numero_de_alunos){
            System.out.print("Nota Aluno "+(count + 1)+": ");
            float nota = reader.nextFloat();
            alunos.add(new Aluno(count+1, nota));
            count++;
        }        
        reader.close(); //Boa pratica importante!
        for (int i = 0; i < alunos.size() - 1; i++) {
            Aluno a1 = alunos.get(i);
            for (int j = i+1; j < alunos.size(); j++) {
                Aluno a2 = alunos.get(j);
                if(a1.getNota() == a2.getNota()) {
                    System.out.println("Aluno "+a1.getId()+" e Aluno "+a2.getId()+" : Aprovados");
                }
            }
        }
    }
}

With a small note that the efficiency of this algorithm is not very good (O(n 2)), but I think that for the case addressed will not be very important.

0

        float maior = 0;
        float num = 0;
        int count = 1;
        int[] alunosMesmaNota = new int[2];

        Scanner entrada = new Scanner(System.in);

        while (count <= 3) {
            System.out.print("Nota Aluno " + count + ": ");
            num = entrada.nextFloat();

            if (num > maior) {
                maior = num;
                alunosMesmaNota[0] = count;
                alunosMesmaNota[1] = 0;
            } else if (num == maior) {
                alunosMesmaNota[1] = count;
            }

            count++;
        }

        System.out.println("O maior numero digitado é: " + maior);
        if (alunosMesmaNota[1] == 0) {
            System.out.println("Pelo aluno " + alunosMesmaNota[0]);
        } else {
            System.out.println("Pelos alunos " + alunosMesmaNota[0] + " e " + alunosMesmaNota[1]);
        }

Browser other questions tagged

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