Schlüsselworte Zählen und Zuweisen von eingelesenen Zahlen

PCFLO

Neues Mitglied
Hallo,

ich soll ein Programm schreiben mit dem man entweder den BMI oder den Notendurchschnitt ausrechnen kann.

Ich habe in meinem Informatik Kurs momentan nur die Grundlagen von Java erlernt (Wörter/Zahlen einlesen, While/For/Do-While Schleife, Verzweigungen).

Mein Problem ist, dass andere ihre Noten/Punkte selber in das Programm eintragen oder bei der Abfrage direkt die Gesamte ausgerechnete Zahl eingeben. Ich möchte aber alles abfragen und bin soweit auch fertig. Problem ist nur das ich nicht weiß, wie ich die Anzahl der benutzen Fächer zählen kann.

Meine Vorstellung war für jede eingebene Zahl eine 1 und für 0 natürlich eine 0 und diese wird am Ende zusammen gezählt was widerrum die Anzahl der Fächer ergibt (So die Theorie). Nur wie schreibe ich den Befehl für das Zählen um den Durchschnitt auszurechnen?

Java:
package programm;
import java.util.Scanner;

public class Notenrechner {

    public static void main(String[] args) {
       
        System.out.println("Möchtest du deinen Noten Durchschnitt ausrechnen?");
        Scanner scan = new Scanner(System.in);
        String Ja;
        Ja = scan.nextLine();
        if (Ja.equals("Ja")) {
            System.out.println("Für Fächer die du nicht Gewählt hast, bitte mit 0 beantworten!");
            System.out.println("");
            System.out.println("Welche Note hast du in Deutsch?");
            Scanner sc = new Scanner(System.in);
            int Note1 = sc.nextInt();
            System.out.println("Welche Note hast du in Mathematik?");
            Scanner cs = new Scanner(System.in);
            int Note2 = cs.nextInt();
            System.out.println("Welche Note hast du in Englisch?");
            Scanner ab = new Scanner(System.in);
            int Note3 = ab.nextInt();
            System.out.println("Welche Note hast du in Sport?");
            Scanner dc = new Scanner(System.in);
            int Note4 = dc.nextInt();
            System.out.println("Welche Note hast du in Geschichte?");
            Scanner gs = new Scanner(System.in);
            int Note5 = gs.nextInt();
            System.out.println("Welche Note hast du in Politik und Wirtschaft?");
            Scanner fr = new Scanner(System.in);
            int Note6 = fr.nextInt();
            System.out.println("Welche Note hast du in Physik?");
            Scanner sd = new Scanner(System.in);
            int Note7 = sd.nextInt();
            System.out.println("Welche Note hast du in Chemie?");
            Scanner hs = new Scanner(System.in);
            int Note8 = hs.nextInt();
            System.out.println("Welche Note hast du in Biologie?");
            Scanner se = new Scanner(System.in);
            int Note9 = se.nextInt();
            System.out.println("Welche Note hast du in Französisch?");
            Scanner sr = new Scanner(System.in);
            int Note10 = sr.nextInt();
            System.out.println("Welche Note hast du in Spanisch?");
            Scanner st = new Scanner(System.in);
            int Note11 = st.nextInt();
            System.out.println("Welche Note hast du in Kunst?");
            Scanner sk = new Scanner(System.in);
            int Note12 = sk.nextInt();
            System.out.println("Welche Note hast du in Musik?");
            Scanner su = new Scanner(System.in);
            int Note13 = su.nextInt();
            System.out.println("Welche Note hast du in Darstellendes Spiel?");
            Scanner rc = new Scanner(System.in);
            int Note14 = rc.nextInt();
            System.out.println("Welche Note hast du in Philosophie?");
            Scanner wc = new Scanner(System.in);
            int Note15 = wc.nextInt();
            System.out.println("Welche Note hast du in Erdkunde?");
            Scanner sx = new Scanner(System.in);
            int Note16 = sx.nextInt();
            System.out.println("Welche Note hast du in Informatik?");
            Scanner sh = new Scanner(System.in);
            int Note17 = sh.nextInt();
           
            int Zahl;
            double Durchschnitt;
            int Anzahl;
       
            Zahl = Note1 + Note2 + Note3 + Note4 + Note5 + Note6 + Note7 + Note8 + Note9 + Note10 + Note11 + Note12 + Note13 + Note14 + Note15 + Note16 + Note17;

            Durchschnitt = Zahl / Anzahl;
           
            scan.close();
            sc.close();
            cs.close();
            ab.close();
            dc.close();
            gs.close();
            fr.close();
            hs.close();
            se.close();
            sd.close();
            sr.close();
            st.close();
            sk.close();
            su.close();
            rc.close();
            wc.close();
            sx.close();
            sh.close();
           
            System.out.println("Dein Durchschnitt beträgt" + Durchschnitt);
        }
        else {
            System.out.println("Dann halt nicht.");
            System.out.println("Schönen Tag noch.");
        }
    }

}

Hilfe wäre echt nett.
PS: Hoffe ich benutze das Forum so richtig. Bin neu hier und habe im Internet keine Lösung auf mein Problem gefunden, da es nirgendswo einen Thread/Thema über das Zählen von Eingaben ging.

Mit freundlichen Grüßen

Florian
 
Hoffe ich benutze das Forum so richtig.
Jawoll. Endlich mal jemand, der Code auf Anhieb mal in die entsprechenden Tags setzt...

Erst mal Allgemeines zum Code:
  1. Bezeichner von Methoden, Variablen und Parametern schreibt man in Java per Konvention in lowerCamelCase. Bezeichner von Typen dagegen in UpperCamelCase.
  2. Du brauchst nur ein Scanner-Objekt
  3. "Ja" ist kein sinnvoller Variablennamen. Nimm was wie "eingabe".
Ich stauche das mal ein wenig zusammen.

Java:
package programm;
import java.util.Scanner;

public class Notenrechner {

    public static void main(String[] args) {
        System.out.println("Möchtest du deinen Noten Durchschnitt ausrechnen?");
        Scanner sc = new Scanner(System.in);
        String eingabe = sc.nextLine();
        if (eingabe.equals("Ja")) {
            System.out.println("Für Fächer die du nicht Gewählt hast, bitte mit 0 beantworten!");
            System.out.println("");
            System.out.println("Welche Note hast du in Deutsch?");
            int note1 = sc.nextInt();
            System.out.println("Welche Note hast du in Mathematik?");
            int note2 = sc.nextInt();
            System.out.println("Welche Note hast du in Englisch?");
            int note3 = sc.nextInt();
            System.out.println("Welche note hast du in Sport?");
            int note4 = sc.nextInt();
            System.out.println("Welche Note hast du in Geschichte?");
            int note5 = sc.nextInt();
            System.out.println("Welche Note hast du in Politik und Wirtschaft?");
            int note6 = sc.nextInt();
            System.out.println("Welche Note hast du in Physik?");
            int note7 = sc.nextInt();
            System.out.println("Welche Note hast du in Chemie?");
            int note8 = sc.nextInt();
            System.out.println("Welche Note hast du in Biologie?");
            int note9 = sc.nextInt();
            System.out.println("Welche Note hast du in Französisch?");
            int note10 = sc.nextInt();
            System.out.println("Welche Note hast du in Spanisch?");
            int note11 = sc.nextInt();
            System.out.println("Welche Note hast du in Kunst?");
            int note12 = sc.nextInt();
            System.out.println("Welche Note hast du in Musik?");
            int note13 = sc.nextInt();
            System.out.println("Welche Note hast du in Darstellendes Spiel?");
            int note14 = sc.nextInt();
            System.out.println("Welche Note hast du in Philosophie?");
            int note15 = sc.nextInt();
            System.out.println("Welche Note hast du in Erdkunde?");
            int note16 = sc.nextInt();
            System.out.println("Welche Note hast du in Informatik?");
            int note17 = sc.nextInt();

            int zahl;
            double durchschnitt;
            int anzahl;

            zahl = note1 + note2 + note3 + note4 + note5 + note6 + note7 + note8 + note9 + note10 + note11 + note12 + note13 + note14 + note15 + note16 + note17;
            durchschnitt = zahl / anzahl;
            System.out.println("Dein Durchschnitt beträgt" + durchschnitt);
        }
        else {
            System.out.println("Dann halt nicht.");
            System.out.println("Schönen Tag noch.");
        }
        sc.close();
    }
}

Beim Berechnen des Durchschnitts musst Du aufpassen: wenn Du eine Ganzzahl durch eine Ganzzahl teilst, ist das Ergebnis eine Ganzzahl und die Nachkommastellen werden abgeschnitten.

Wie kommst Du nun zur Anzahl der eingegebenen Noten? Naja, wenn eine Note > 0 eingegeben wurde, zählst Du die Anzahl um eins hoch, ansonsten nicht. Das ist natürlich mit Schreibaufwand verbunden, weil Du somit 17 if-Abfragen einbauen musst.

Man kann die 1 bzw. 0, die man auf die Anzahl anrechnen muss, auch direkt aus der Note berechnen. Dazu macht man sich z. B. die vorhin genannte Eigenschaft zu Nutze, dass beim Teilen von Ganzzahlen die Nachkommastellen abgeschnitten werden. Darauf will ich aber jetzt nicht weiter eingehen.

Grundsätzlich wäre der ganze Code wesentlich einfacher, wenn Du z. B. Arrays verwenden würdest. Allerdings weiß ich nicht, ob Du das darfst. Ebenso wenig weiß ich, ob Du eigene Methoden schreiben darfst. Daher erst mal ohne.

Ach, ja und noch was, was die Anzahl bzw. die Berechnung des Durchschnitts betrifft: Division durch 0 ist nicht erlaubt...
 

Zurück
Oben