Dificuldade no JAVA
08/05/2020
0
Pessoal tô fazendo um programa que calcule a média aritmética de 5 notas, desconsiderando a menor nota...
Eu pensei em utilizar um laço para não precisar utilizar 5 inputs mas ainda tô meio perdido em como comparar as 5 notas e eliminar a menor delas..
Eu pensei em utilizar um laço para não precisar utilizar 5 inputs mas ainda tô meio perdido em como comparar as 5 notas e eliminar a menor delas..
Cristian Guilherme
Curtir tópico
+ 0
Responder
Post mais votado
13/05/2020
package br.com.devmedia; /** * * @author rodrigo.dantas * */ public class Aluno { private String nome; private Float nota; public Aluno() { } public Aluno(String nome, Float nota) { this.nome = nome; this.nota = nota; } public String getNome() { return nome; } public void setNome(String nome) { this.nome = nome; } public Float getNota() { return nota; } public void setNota(Float nota) { this.nota = nota; } }
package br.com.devmedia; import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * * @author rodrigo.dantas * */ public class Exemplo { public static void main(String[] args) { List<Aluno> alunos = new ArrayList<>(); Scanner s = new Scanner(System.in); do { String nome = s.nextLine(); Float nota = s.nextFloat(); alunos.add(new Aluno(nome, nota)); System.out.print("Deseja continuar? "); } while (getResponse(s.nextLine())); int position = getSmaller(alunos); Float soma = 0.0F; for (int i = 0; i < args.length; i++) { if (position == i) continue; soma += alunos.get(i).getNota(); } Float media = soma/(alunos.size()-1); s.close(); } /** * Pega a posição da menor nota a partir de uma {@link List} de {@link Aluno}.<br> * Considerar somente um vez se o menor valor se repetir. * @param alunos Lista de {@link Aluno}. * @return A posição da menor nota. */ private static int getSmaller(List<Aluno> alunos) { int position = 0; for (int i = 1; i < alunos.size(); i++) { if (alunos.get(i).getNota() < alunos.get(position).getNota()) position = i; } return position; } /** * Valida a resposta do usuário. * @param msg * @return true or false. */ private static boolean getResponse(String msg) { return msg.toLowerCase().matches("(\\\\W|^)(sim|s)(\\\\W|$)"); } }
Rodrigo Dantas
Responder
Clique aqui para fazer login e interagir na Comunidade :)