Abimael_Guesso 13 de mai. de 2021
public static void main(String[] args) {
ControleProva boletim = new ControleProva ();
Prova bimestre = boletim . Nota ();
System . out . println ( bimestre . getNota1 ());
System . out . println ( bimestre . getNota2 ());
System . out . println ( bimestre . getNota3 ());
System . out . println ( bimestre . calcular ());
}
assim tá na minha main
Abimael_Guesso 13 de mai. de 2021
é para retornar a média dos dois maiores números
davidbuzatto 13 de mai. de 2021
Três formas:
int n1 = 10 ;
int n2 = 5 ;
int n3 = 3 ;
int soma = 0 ;
int media ;
if ( n1 <= n2 && n2 <= n3 ) {
soma = n2 + n3 ;
} else if ( n1 <= n3 && n3 <= n2 ) {
soma = n2 + n3 ;
} else if ( n2 <= n1 && n1 <= n3 ) {
soma = n1 + n3 ;
} else if ( n2 <= n3 && n3 <= n1 ) {
soma = n1 + n3 ;
} else if ( n3 <= n1 && n1 <= n2 ) {
soma = n1 + n2 ;
} else if ( n3 <= n2 && n2 <= n1 ) {
soma = n1 + n2 ;
}
media = soma / 2 ;
// ou
int temp ;
if ( n1 > n2 ) {
temp = n1 ;
n1 = n2 ;
n2 = temp ;
}
// aqui vc tem a garantia que n1 é maior que n2
if ( n1 > n3 ) {
temp = n1 ;
n1 = n3 ;
n3 = temp ;
}
// aqui vc tem a garantia que n1 é maior que n2 e n3
if ( n2 > n3 ) {
temp = n2 ;
n2 = n3 ;
n3 = temp ;
}
// aqui vc tem a garantia que n1 é maior que n2 e n3 e que n2 é maior que n3
// vc tem algo assim , obrigatoriamente n3 <= n2 <= n1
media = ( n2 + n1 ) / 2 ;
// ou ainda :D
int [] ns = new int []{ n1 , n2 , n3 };
Arrays .sort ( ns ) ;
media = ( ns [1 ] + ns [2 ] ) / 2 ;
Abimael_Guesso 13 de mai. de 2021
public float calcular() {
//8 9 10
float soma = 0 ;
if ( nota1 < = nota2 && nota2 < = nota3 ) {
soma = nota2 + nota3 ;
} else if ( nota1 < = nota3 && nota3 < = nota2 ) {
soma = nota2 + nota3 ;
} else if ( nota2 < = nota1 && nota1 < = nota3 ) {
soma = nota1 + nota3 ;
} else if ( nota2 < = nota3 && nota3 < = nota1 ) {
soma = nota1 + nota3 ;
} else if ( nota3 < = nota1 && nota1 < = nota2 ) {
soma = nota1 + nota2 ;
} else if ( nota3 < = nota2 && nota2 < = nota1 ) {
soma = nota1 + nota2 ;
}
return soma / 2 ;
}
davidbuzatto
obrigado cara, me ajudou muito, segue aqui.