Ich brauche Hilfe bei dieser Aufgabe

Leo0909

Neues Mitglied
Hallo, ich soll diese aufgabe lösen aber wenn ich das richtig verstehe muss ich da zwei vermiedene Datentypen in eine ausgäbe packen.
wie mache ich das oder liege ich total falsch. ( Bin blutiger Anfänger)

danke für die Hilfe
 

Anhänge

  • Bildschirmfoto 2021-04-11 um 17.59.49.png
    Bildschirmfoto 2021-04-11 um 17.59.49.png
    562,4 KB · Aufrufe: 48
  • Bildschirmfoto 2021-04-11 um 17.59.28.png
    Bildschirmfoto 2021-04-11 um 17.59.28.png
    351 KB · Aufrufe: 43
zwei verschiedene Datentypen in eine Ausgabe packen.
Das wären Integer und Double. Auf die Schnelle *könnte* dieses (mit Eclipse und Java 15 erstellte) Programm das Prinzip demonstrieren und zum eigenen Erweitern dienen:
Java:
import java.util.Scanner;

public class Kino {

    int Alter;
    int FSK = 2;
    double Vollpreis = 6.30;
    int Reihe;
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Kino kino = new Kino();
        Scanner scan = new Scanner(System.in);
        System.out.print("Alter eingeben: (FSK: " +kino.FSK +")");

        kino.Alter = scan.nextInt();
System.out.println("Reihe eingeben (1-12): ");
kino.Reihe = scan.nextInt();
        // Closing Scanner after the use
        scan.close();
        
        if(kino.Alter < kino.FSK) {
            System.out.println("zu jung...");
            
        }
        else {
            if (kino.Alter < 6) {
                kino.Vollpreis = kino.Vollpreis * 0.5; 
            }
            if ((kino.Alter <=12) && (kino.Alter  >=6 )) {
                kino.Vollpreis = kino.Vollpreis *0.75;
            }
            if ((kino.Alter >=13) && (kino.Alter <=16)) {
                kino.Vollpreis *= 0.9;
            }
            switch(kino.Reihe) {
            case 1,2,3,4: kino.Vollpreis *= 0.7; System.out.println("Preis: " + kino.Vollpreis); break;
            case 5,6,7,8: System.out.println("Preis: " + kino.Vollpreis); break;
            case 9,10,11: kino.Vollpreis *= 1.3; System.out.println("Preis: " + kino.Vollpreis); break;
            case 12: kino.Vollpreis = kino.Vollpreis *1.5; System.out.println("Preis: " + kino.Vollpreis); break;
            }
        }
          }
}
 
Etwas überarbeitet
[CODE lang="java" highlight="7-20"]import java.util.Scanner;

public class Kino {
private static Scanner input = new Scanner(System.in);

public static void main(String[] args) {
Movie movie = new Movie();
System.out.print("Alter eingeben: (FSK: " + movie.fsk + ") > ");
movie.age = input.nextInt();
System.out.print("Reihe eingeben (1-12): > ");
movie.row = input.nextInt();
if (movie.age < movie.fsk)
System.out.println("zu jung...");
else {
try {
System.out.println(String.format("Preis: %.2f", getPrice(movie)));
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}

private static double getPrice(Movie kino) throws IllegalArgumentException {
if (kino.row < 1 || kino.row > 12)
throw new IllegalArgumentException("Ungueltige Sitzreihe '" + kino.row + "'");
double basicPrice = getAgeDiscount(kino.basicPrice, kino.age);
if (kino.row < 5)
return basicPrice * .7;
if (kino.row < 9)
return basicPrice;
if (kino.row < 12)
return basicPrice * 1.3;
return basicPrice * 1.5;
}

private static double getAgeDiscount(double basicPrice, int age) {
if (age < 6)
return basicPrice * .5;
if (age <= 12)
return basicPrice * .75;
if (age <= 16)
return basicPrice * .9;
return basicPrice;
}
}[/CODE]
[CODE lang="java" title="Containerklasse"]public class Movie {
public int age;
public int fsk = 2;
public double basicPrice = 6.30;
public int row;
}[/CODE]
 

Zurück
Oben