1
C, C++, C#, Java, Visual Basic, HTML, PHP, CSS, Javascript, Ajax, Joomla, MySql y más / Re:Registrar 5 alumnos con su materia y 3 calificaciones
« en: 27 de Agosto 2019, 20:19 »
Usando tu código me quedó así:
La clases notas:
Y la clase donde esta el menu "Deber1" :
La clases notas:
Código: [Seleccionar]
package deber1;
import java.util.Scanner;
public class notas {
Scanner teclado = new Scanner(System.in);
static String[] nombres = new String[5];
static String[] materias = new String[5];
static double[][] notas = new double[5][3];
public void datos() {
for (int i = 0; i < 5; i++) {
System.out.println("Registrando alumno #" + (i + 1));
System.out.print("Nombre: ");
nombres[i] = teclado.nextLine();
System.out.print("Materia: ");
materias[i] = teclado.nextLine();
System.out.print("Nota 1: ");
notas[i][0] = Double.parseDouble(teclado.nextLine());
System.out.print("Nota 2: ");
notas[i][1] = Double.parseDouble(teclado.nextLine());
//La Nota 3 se autocalcula según formula.
notas[i][2] = (((((notas[i][0] + notas[i][1]) / 2) + 0.5)) * 2) / 2;
if(notas[i][2]>10){
notas[i][2] =notas[i][2]- (notas[i][2] - 10);
System.out.println("Valor autocalculado para Nota 3: " + notas[i][2]);
}else{
System.out.println("Valor autocalculado para Nota 3: " + notas[i][2]);
}
System.out.println("Alumno registrado...\n");
}
}
public void notaPromedioMasAlta() {
/*
* Calculamos el promedio de cada Alumno
* y nos quedaremos con la más alta.
*/
double masAlta = 0;
int alumno = 0;
for (int i = 0; i < notas.length; i++) {
double sumaNotas = notas[i][0] + notas[i][1] + notas[i][2];
double promedio = sumaNotas / 3;
if (promedio > masAlta) {
masAlta = promedio;
alumno = i;
}
}
System.out.printf("\nNota promedio más alta: %.2f\n", masAlta);
System.out.println("Alumno: " + nombres[alumno]);
}
public void tresNotasMasBajas() {
//Construimos array con todas las notas
double[] todasNotas = new double[15]; //5 alumnos, 3 notas: 5x3 = 15 notas
int z = 0; //Indice para el array unidimenional
for (int i = 0; i < notas.length; i++) {
for (int j = 0; j < notas[0].length; j++) {
todasNotas[z] = notas[i][j];
z++;
}
}
//Array construido, ordenamos de menor a mayor
for (int i = 0; i < todasNotas.length - 1; i++) {
for (int j = 0; j < todasNotas.length - i - 1; j++) {
if (todasNotas[j + 1] < todasNotas[j]) {
double aux = todasNotas[j + 1];
todasNotas[j + 1] = todasNotas[j];
todasNotas[j] = aux;
}
}
}
//Array ordenado, mostramos los tres primeros
System.out.println("\nLas tres notas más bajas son:");
for (int i = 0; i < 3; i++) {
System.out.println("- " + todasNotas[i]);
}
}
public void ordenaNota1() {
/*
* Construimos array solo con notas 1
* y ordenamos de mayor a menor
*/
double[] notas1 = new double[5];
for (int i = 0; i < 5; i++) {
notas1[i] = notas[i][0];
}
//Ordenamos
for (int i = 0; i < notas1.length - 1; i++) {
for (int j = 0; j < notas1.length - i - 1; j++) {
if (notas1[j + 1] > notas1[j]) {
double aux = notas1[j + 1];
notas1[j + 1] = notas1[j];
notas1[j] = aux;
}
}
}
//Mostramos
System.out.println("\nNotas 1 de MAYOR a MENOR:");
for (int i = 0; i < 5; i++) {
System.out.print(notas1[i] + " ");
}
}
}Y la clase donde esta el menu "Deber1" :
Código: [Seleccionar]
package deber1;
import java.util.Scanner;
public class Deber1 {
public static void main(String[] args) {
int opcion;
notas obj = new notas();
Scanner teclado = new Scanner(System.in);
do {
System.out.println("\n\n\t\tMENU OPCIONES\n");
System.out.println("[1] Ingresar los datos");
System.out.println("[2] Mostrar nota promedio más alta.");
System.out.println("[3] Mostrar las 3 notas más bajas.");
System.out.println("[4] Ordenar de mayor a menor Nota 1.");
System.out.println("[5] SALIR.");
System.out.print("Elija opcion: ");
opcion = Integer.parseInt(teclado.nextLine());
switch (opcion) {
case 1:
obj.datos();
break;
case 2:
obj.notaPromedioMasAlta();
break;
case 3:
obj.tresNotasMasBajas();
break;
case 4:
obj.ordenaNota1();
break;
case 5:
System.out.println("\n\t\tFIN DE PROGRAMA");
break;
default:
System.out.println("\nOpcion equivocada. Escoja entre [1-4]");
}
} while (opcion != 5);
}
}
RSS