Korrektur fehlerhafter Java-Programme mit Notenberechnung
Korrigierte Java-Beispiele
Beispiel 1: Notenberechnung
public class Beispiel {
public static void main(String[] args) {
double n1, n2, kontrollen, praesentation, notaExamen, notaFinal;
n1 = Leer.leerReal("Geben Sie Kontrollnote 1");
n2 = Leer.leerReal("Geben Sie Kontrollnote 2");
kontrollen = Leer.leerReal("Bitte geben Sie die Kontrollen ein");
praesentation = n1 * 0.35 + n2 * 0.35 + kontrollen * 0.3;
System.out.println("Note der Präsentation: " + praesentation);
if (praesentation < 3.5) {
System.out.println("Nicht bestanden");
} else if (praesentation >= 5.0) {
System.out.println("Exzellent");
} else {
System.out.println("Sie müssen die Prüfung in Erwägung ziehen");
notaExamen = Leer.leerReal("Geben Sie die Prüfungsnote ein");
notaFinal = 0.7 * notaExamen + praesentation * 0.3;
if (notaFinal >= 3.95) {
System.out.println("Bestanden");
} else {
System.out.println("Nicht bestanden");
}
}
}
}
Beispiel 2: Gewichteter Durchschnitt
public class Ejercicio2 {
public static void main(String[] args) {
double n1 = Leer.leerReal("Geben Sie Note 1 (1..7)");
double n2 = Leer.leerReal("Geben Sie Note 2 (1..7)");
double p1 = Leer.leerReal("Geben Sie Prozent 1 (0..1)");
double p2 = Leer.leerReal("Geben Sie Prozent 2 (0..1)");
double promedio = n1 * p1 + n2 * p2;
if ((p1 + p2) > 1 || (p1 + p2) < 0 || p1 < 0 || p1 > 1 || p2 < 0 || p2 > 1) {
System.out.println("Prozent falsch");
} else {
if (n1 < 1 || n1 > 7 || n2 < 1 || n2 > 7) {
System.out.println("Noten falsch");
} else {
System.out.println("Durchschnitt ist: " + promedio);
}
}
}
}
Beispiel 3: Gerade und ungerade Ziffern zählen
public class Beispiel {
public static int cuentaPares(int num) {
int resultado = 0;
while (num != 0) {
int digito = num % 10;
if (digito != 0 && digito % 2 == 0) {
resultado = resultado + 1;
}
num = num / 10;
}
return resultado;
}
public static int cuentaImpares(int num) {
int resultado = 0;
while (num != 0) {
int digito = num % 10;
if (digito != 0 && digito % 2 != 0) {
resultado = resultado + 1;
}
num = num / 10;
}
return resultado;
}
public static void main(String[] args) {
int numero = Leer.leerEntero("Zahl eingeben");
System.out.println("Die Zahl " + numero + " hat " + cuentaPares(numero) + " gerade Ziffern");
System.out.println("Die Zahl " + numero + " hat " + cuentaImpares(numero) + " ungerade Ziffern");
}
}
Beispiel 4: Minimum, Maximum und Statistik
public class Calificaciones {
public static void mayorMenor(int[] listaCualquiera) {
int menor = 8;
int mayor = 0;
for (int i = 0; i < listaCualquiera.length; i++) {
if (listaCualquiera[i] > mayor) {
mayor = listaCualquiera[i];
}
if (listaCualquiera[i] < menor) {
menor = listaCualquiera[i];
}
}
System.out.println("Die kleinste Note ist: " + menor);
System.out.println("Die größte Note ist: " + mayor);
}
public static void imprimeEstadisticas(int[] listaCualquiera) {
int azul = 0;
int rojo = 0;
double promedio = 0;
for (int i = 0; i < listaCualquiera.length; i++) {
promedio = promedio + listaCualquiera[i];
if (listaCualquiera[i] < 4) {
rojo++;
} else {
azul++;
}
}
System.out.println("Blau: " + azul);
System.out.println("Rot: " + rojo);
System.out.println("Durchschnitt: " + promedio / listaCualquiera.length);
}
public static void imprimir(int[] listaCualquiera) {
for (int i = 0; i < listaCualquiera.length; i++) {
System.out.println("Note " + i + " ist " + listaCualquiera[i]);
}
}
public static void main(String[] args) {
int lista1[] = new int[10];
int lista2[] = new int[8];
System.out.println("Fülle lista1");
for (int i = 0; i <= 9; i++) {
lista1[i] = Leer.leerEntero("Bitte Note eingeben " + i);
}
System.out.println("Fülle lista2");
for (int i = 0; i <= 7; i++) {
lista2[i] = Leer.leerEntero("Bitte Note eingeben " + i);
}
imprimir(lista1);
imprimir(lista2);
imprimeEstadisticas(lista1);
imprimeEstadisticas(lista2);
mayorMenor(lista1);
mayorMenor(lista2);
}
}
Beispiel 5: Liste nach Namen und Noten sortieren
public class ListaDeCurso {
public static void muestraDesordenada(String nombres[], int notas[]) {
for (int i = 0; i < 10; i++) {
System.out.println("Name: " + nombres[i] + " - Note " + notas[i]);
}
}
public static void muestraOrdenadoPorNombre(String nombres[], int notas[]) {
for (int i = 0; i < (nombres.length - 1); i++) {
for (int j = i + 1; j < nombres.length; j++) {
if (nombres[i].compareTo(nombres[j]) > 0) {
int nota = notas[j];
notas[j] = notas[i];
notas[i] = nota;
String nombre = nombres[j];
nombres[j] = nombres[i];
nombres[i] = nombre;
}
}
}
for (int i = 0; i < 10; i++) {
System.out.println("Name: " + nombres[i] + " - Note " + notas[i]);
}
}
public static void main(String[] args) {
String nombres[] = new String[10];
int notas[] = new int[10];
for (int i = 0; i < 10; i++) {
nombres[i] = Leer.leer("Geben Sie Name Student " + i);
notas[i] = Leer.leerEntero("Bitte Note eingeben Student " + i);
}
System.out.println("Unsortierte Liste");
muestraDesordenada(nombres, notas);
System.out.println("Liste sortiert nach Namen");
muestraOrdenadoPorNombre(nombres, notas);
System.out.println("Liste sortiert nach Noten");
muestraOrdenadoPorNota(nombres, notas);
}
}
Beispiel 6: Schleifen mit Abbruch
public class Ciclos2 {
public static void main() {
System.out.println("So werden Zyklen verwendet");
int i = 1;
inicio: while (i <= 4) {
System.out.println("i ist " + i);
int j = i;
while (j <= 4) {
if (i > 2) {
continue inicio;
} else {
System.out.println("i ist " + i + " j geht " + j);
}
j = j + 1;
}
i = i + 1;
}
}
}
Beispiel 7: Eingabe zweier Arrays
public class Calificaciones {
public static void main(String[] args) {
int lista1[] = new int[10];
int lista2[] = new int[8];
System.out.println("Fülle lista1");
for (int i = 0; i <= 9; i++) {
lista1[i] = Leer.leerEntero("Bitte Note eingeben " + i);
}
System.out.println("Fülle lista2");
for (int i = 0; i <= 7; i++) {
lista2[i] = Leer.leerEntero("Bitte Note eingeben " + i);
}
}
}
Beispiel 8: Schleife mit Sprung zum Zyklus
public class Ciclo {
public static void main() {
System.out.println("Beispiel-Zyklus");
inicio: for (int i = 1; i <= 4; i++) {
System.out.println("i ist " + i);
for (int j = 1; j <= 5; j++) {
if (i > 2) {
continue inicio;
} else {
System.out.println("i ist " + i + " j geht " + j);
}
}
}
}
}
Beispiel 9: Ausgabe und Statistik zweier Listen
public class Calificaciones {
public static void imprimeEstadisticas(int[] listaCualquiera) {
int azul = 0;
int rojo = 0;
double promedio = 0;
for (int i = 0; i < listaCualquiera.length; i++) {
promedio = promedio + listaCualquiera[i];
if (listaCualquiera[i] < 4) {
rojo++;
} else {
azul++;
}
}
System.out.println("Blau: " + azul);
System.out.println("Rot: " + rojo);
System.out.println("Durchschnitt: " + promedio / listaCualquiera.length);
}
public static void imprimir(int[] listaCualquiera) {
for (int i = 0; i < listaCualquiera.length; i++) {
System.out.println("Note " + i + " ist " + listaCualquiera[i]);
}
}
public static void main(String[] args) {
int lista1[] = new int[10];
int lista2[] = new int[8];
System.out.println("Fülle lista1");
for (int i = 0; i <= 9; i++) {
lista1[i] = Leer.leerEntero("Bitte Note eingeben " + i);
}
System.out.println("Fülle lista2");
for (int i = 0; i <= 7; i++) {
lista2[i] = Leer.leerEntero("Bitte Note eingeben " + i);
}
imprimir(lista1);
imprimir(lista2);
imprimeEstadisticas(lista1);
imprimeEstadisticas(lista2);
}
}