Java :: Aufgabe #4 :: Lösung #1
15 Lösungen

#4
Zinseszinsberechnung und Ausgabe
Anfänger - Java
von Gustl
- 12.08.2012 um 14:59 Uhr
Schreiben Sie ein Programm zur Zinseszinsberechnung. Das Konsolenprogramm soll den anzulegenden Geldbetrag, den Jahreszins und die Laufzeit in Jahren abfragen. Danach soll für jedes Laufjahr der Geldbetrag mit Zinseszins ausgegeben werden.
Etwa so:
Etwa so:
Konsolenausgabe:
Geldbetrag in Euro: 150
Jahreszins (0.05 = 5%): 0.04
Laufzeit in Jahren: 3
Wert nach 1 Jahr: 156,00 Euro
Wert nach 2 Jahren: 162,24 Euro
Wert nach 3 Jahren: 168,73 Euro
#1

von phobos (80 Punkte)
- 12.09.2012 um 09:18 Uhr

package trainyourprogrammer.phobos.nr04; import java.text.DecimalFormat; import java.util.InputMismatchException; import java.util.Scanner; public class Zinsberechnung { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { double geld = doubleEingabe("Geldbetrag in Euro:"); double zins = doubleEingabe("Jahreszins (0,05=5%):"); int laufzeit = intEingabe("Laufzeit in Jahren: "); DecimalFormat df = new DecimalFormat ( ".00" ); for(int i=1;i<=laufzeit;i++) { geld+=geld*zins; System.out.println("Wert nach "+i+" Jahren: "+df.format(geld)+" Euro"); } } public static int intEingabe(String txt) throws IllegalStateException { int result; while (true) { System.out.print(txt); try { result = sc.nextInt(); break; } catch (InputMismatchException ime) { sc.nextLine(); System.out.println("Du musst eine ganze Zahl eingeben."); } } return result; } public static double doubleEingabe(String txt) throws IllegalStateException { double result; while (true) { System.out.print(txt); try { result = sc.nextDouble(); break; } catch (InputMismatchException ime) { sc.nextLine(); System.out.println("Du musst eine Zahl eingeben."); } } return result; } }
Kommentare:
Für diese Lösung gibt es noch keinen Kommentar
Seite 1 von 0
1