Java :: Aufgabe #4

15 Lösungen Lösungen öffentlich

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:

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

Lösungen:

vote_ok
von phobos (80 Punkte) - 12.09.2012 um 09:18 Uhr
Quellcode ausblenden Java-Code
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;
    }

}
vote_ok
von bossik (160 Punkte) - 13.09.2012 um 22:34 Uhr
Quellcode ausblenden Java-Code
import java.util.Scanner;


public class Zinsenberechnung {

	
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		boolean eingaben = false;
		double eingabeGeld, eingabeJahrZins;
		int eingabeJahre;
		String ausgabe;
		double result = 0, zinsen;
		
			System.out.print("Geldbetrag in Euro: ");
			eingabeGeld = s.nextDouble();
			System.out.print("Jahreszins (0,05 = 5%): ");
			eingabeJahrZins = s.nextDouble();
			System.out.print("Laufzeit in Jahren: ");
			eingabeJahre = s.nextInt();
			
			for(int i=1; i<eingabeJahre+1;i++){
				
				
				if(i==1){
					zinsen = eingabeGeld*eingabeJahrZins;
				    result = Math.round((eingabeGeld+zinsen)*100.)/100.;
					ausgabe = "Wert nach "+i+" Jahr: "+result+" EURO";
					System.out.println(ausgabe);
				}else{
					zinsen = result*eingabeJahrZins;
				    result = Math.round((result+zinsen)*100.)/100.;
					ausgabe = "Wert nach "+i+" Jahren: "+result+" EURO";
					System.out.println(ausgabe);
				}
			}
	}

}


Konsolenausgabe:

Geldbetrag in Euro: 1000
Jahreszins (0,05 = 5%): 0,03
Laufzeit in Jahren: 10
Wert nach 1 Jahr: 1030.0 EURO
Wert nach 2 Jahren: 1060.9 EURO
Wert nach 3 Jahren: 1092.73 EURO
Wert nach 4 Jahren: 1125.51 EURO
Wert nach 5 Jahren: 1159.28 EURO
Wert nach 6 Jahren: 1194.06 EURO
Wert nach 7 Jahren: 1229.88 EURO
Wert nach 8 Jahren: 1266.78 EURO
Wert nach 9 Jahren: 1304.78 EURO
Wert nach 10 Jahren: 1343.92 EURO
vote_ok
von 23Java (510 Punkte) - 17.09.2012 um 11:33 Uhr
Quellcode ausblenden Java-Code
import java.util.Scanner;
import java.text.*;

public class Zins {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		DecimalFormat f = new DecimalFormat("#0.00");

		int lz = 0;
		double zs = 0, b = 0;
		boolean lzset = false, zsset = false, bset = false;

		while(!lzset) {
			System.out.println("Laufzeit eingeben:");
			
			if(s.hasNextInt()) {
				lz = s.nextInt();
				lzset = true;
			} else {
				System.out.println("Bitte ganzzahlige Laufzeit eingeben!");
			}
		}

		while(!zsset) {
			System.out.println("Zinssatz eingeben:");
			
			if(s.hasNextDouble()) {
				zs = s.nextDouble();
				zsset = true;
			} else {
				System.out.println("Bitte Zinssatz eingeben! (zB. 0,03)");
			}
		}

		while(!bset) {
			System.out.println("Betrag eingeben:");
			
			if(s.hasNextDouble()) {
				b = s.nextDouble();
				bset = true;
			} else {
				System.out.println("Bitte Betrag eingeben!");
			}
		}

		System.out.println("Laufzeit: " + lz + "\n" +
					"Zinssatz: " + zs + "\n" +
					"Geldbetrag: " + b);
		for(int i = 1; i <= lz; i++) {
			b = b + b*zs;
			System.out.println("Jahr: " + i + " - Geldbetrag: " + f.format(b));
		}
	}
}


Konsolenausgabe:

Laufzeit eingeben:
10
Zinssatz eingeben:
0,075
Betrag eingeben:
1000
Laufzeit: 10
Zinssatz: 0.075
Geldbetrag: 1000.0
Jahr: 1 - Geldbetrag: 1075,00
Jahr: 2 - Geldbetrag: 1155,62
Jahr: 3 - Geldbetrag: 1242,30
Jahr: 4 - Geldbetrag: 1335,47
Jahr: 5 - Geldbetrag: 1435,63
Jahr: 6 - Geldbetrag: 1543,30
Jahr: 7 - Geldbetrag: 1659,05
Jahr: 8 - Geldbetrag: 1783,48
Jahr: 9 - Geldbetrag: 1917,24
Jahr: 10 - Geldbetrag: 2061,03
vote_ok
von Jurom (1070 Punkte) - 19.10.2012 um 10:07 Uhr
Quellcode ausblenden Java-Code
//©2012 by Julius J. Hoffmann
//written with Eclipse

import java.util.*;			//Importiert Scanner zum Einlesen der Eingabe


public class Zins {		// Java-Klasse mit main-Methode ("ausführende Klasse")							

	public static void main(String[] args) 				//main-Funktion
	{
		System.out.print("Geldbetrag in Euro: ");		//Gibt den String auf dem Bildschirm aus
		Scanner s = new Scanner(System.in);				//legt das Scanner-Objekt an
		float Ko = s.nextFloat();						//liest die manuelle Eingabe ein und speichert sie in der Variablen Ko
		System.out.print("Jahreszins (0,05 = 5%): ");	//Gibt den String auf dem Bildschirm aus
		float p = s.nextFloat();						//liest die manuelle Eingabe ein und speichert sie in der Variablen p
		System.out.print("Laufzeit in Jahren: ");		//Gibt den String auf dem Bildschirm aus
		int n = s.nextInt();							//liest die manuelle Eingabe ein und speichert sie in der Variablen n
		
		/**
		 *Initiiert einer for-Schleife
		 * int i = 1 - Initialisiert den i-Operator (mit Startwert 1),
		 * (i<=n) - der, bis er genausogroß wie n ist,
		 * (i++) - nach jedem Schleifendurchlauf um 1 hochzählt 
		 */
		for (int i = 1;i <= n; i++)											//s.o.
		{
			Ko = Ko+(Ko*p);													//einf. Zinsrechnung mit Iteration
			System.out.println("Wert nach " + i + " Jahren: " + Ko + "€");	//gibt den Betrag nach i-Jahren aus
		}	//Beendet for-Schleife
	}		//Beendet main-Methode
}			//Schliesst class

vote_ok
von ElPapito (2690 Punkte) - 09.05.2015 um 01:29 Uhr
Quellcode ausblenden Java-Code

/**
 * @author ElPapito
 * @date 09.05.2015
 */

import java.util.Scanner;

public class ZinseszinsberechnungUndAusgabe {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);

		System.out.print("Geldbetrag in Euro: ");
		double value = scanner.nextDouble();
		System.out.print("Jahreszins (0,05 = 5%): ");
		double rate = scanner.nextDouble();
		System.out.print("Laufzeit in Jahren: ");
		int time = scanner.nextInt();
		System.out.println();
		scanner.close();

		for (int i = 1; i <= time; i++) {
			value = value * (1 + rate);

			// Zahl für Ausgabe auf 2 Nachkommastellen runden
			double output = (int) (Math.round(value * 100)) / 100.0;

			System.out.print("Wert nach " + i + " Jahr");
			if (i > 1) {
				System.out.print("en");
			}
			System.out.print(":\t" + output);
			if ((int) (output * 100) % 10 == 0) {
				System.out.print("0");
			}
			System.out.println(" Euro");
		}
	}

}

vote_ok
von itsfalse (60 Punkte) - 23.01.2016 um 17:07 Uhr
Quellcode ausblenden Java-Code
/*	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.
 */

/*	author: itsfalse 	
 *  23.01.2016
 *  Geschrieben mit Eclipse.
 */ 


import java.util.Scanner;

public class No4 {
	
	public static void main(String[] args) {
	
		double endbetrag = 0;
		System.out.print("Geldbetrag in Euro: ");
		Scanner s = new Scanner(System.in);
		double betrag = s.nextDouble();
		betrag = Math.round(betrag * 100) / 100;
		
		System.out.print("Jahreszins: ");
		double zins = s.nextDouble();
		
		System.out.print("Laufzeit in Jahren: ");
		int jahre = s.nextInt();
		
		endbetrag = betrag * Math.pow((1 + (zins / 100)),jahre);
		
		
		System.out.println("Wert nach 1 Jahr: "+ Math.round((betrag * Math.pow((1 + (zins / 100)),1))*100.00) / 100.00 + " Euro" );
		System.out.println("Wert nach 2 Jahren: "+ Math.round((betrag * Math.pow((1 + (zins / 100)),2))*100.00) / 100.00 + " Euro" );
		System.out.println("Wert nach 3 Jahren: "+ Math.round((betrag * Math.pow((1 + (zins / 100)),3))*100.00) / 100.00 + " Euro" );
		System.out.println("Wert nach " + jahre + " Jahren: "+ Math.round(endbetrag*100) /100 + " Euro");
	}

}


vote_ok
von HaraldK (260 Punkte) - 21.02.2016 um 21:46 Uhr
Quellcode ausblenden Java-Code
/* @Author H.K.
 * @Date 21.02.2016
 * 
 * Programmbeschreibung:
 * Programm zur Zinseszinsberechnung
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class mainprogramv2 {

	public static void main ( String args[] ) throws IOException {
		double[] zinseszins = new double[1];
		double[] allparameters = new double[3];
		
		allparameters = fcabfrage(allparameters);
		zinseszins = fczinseszins(allparameters, zinseszins);
	}

	public static double[] fczinseszins(double allparameters[], double zinseszins[]) {
		double gesamtbetrag = allparameters[0];
		for (int i = 1; i <= allparameters[1]; i++) {
			zinseszins[0] = allparameters[0] * (1+((allparameters[2]*100)/100)) - allparameters[0];
			gesamtbetrag = zinseszins[0] + allparameters[0];
			allparameters[0] = gesamtbetrag;
			System.out.println("Wert nach Jahr " +i +": " +gesamtbetrag);
		}
		return new double[] { allparameters[0], allparameters[1], allparameters[2] };
	}

	private static double[] fcabfrage(double allparameters[]) throws IOException {
		double[] eingabe = new double[1];

		while (allparameters[0] == 0) {
			allparameters[0] = eingabe(allparameters[0], eingabe[0]);
			eingabe[0]++;
		}
		while (allparameters[1] == 0) {
			allparameters[1] = eingabe(allparameters[1], eingabe[0]);
			eingabe[0]++;
		}
		while (allparameters[2] == 0) {
			allparameters[2] = eingabe(allparameters[2],  eingabe[0]);
			eingabe[0]++;
		}
		return allparameters;
	}

	private static double eingabe(double eingabe, double verify) throws IOException {
		while (eingabe == 0) {
		 if (verify == 0 || verify == 1) {
			if (verify == 0) {
				System.out.println("Bitte den Geldbetrag in Euro angeben: ");
			}
			if (verify == 1) {
				System.out.println("Bitte die Laufzeit in Jahren angeben: ");
			}
			BufferedReader input = new BufferedReader ( new InputStreamReader ( System.in ) );
		    String inputString = input.readLine();
		    if (inputString.matches("-?\\d+?")) {
		    	eingabe = Integer.parseInt(inputString);
			}
		    else {
		    	System.out.println("Eingabe ist keine Zahl!");
		    }
		}
		if (verify == 2) {
		System.out.println("Bitte die Zinsen angeben (5% = 0.05): ");
		BufferedReader input = new BufferedReader ( new InputStreamReader ( System.in ) );
	    String inputString = input.readLine();
	    if (inputString.matches("-?\\d\\.\\d+?")) {
	    	eingabe = Double.parseDouble(inputString);
		}
	    else {
	    	System.out.println("Eingabe keine Zahl oder ohne Punkt!");
	    }
		}
		}
		return eingabe;
	}
}



Ausgabe:

Konsolenausgabe:



Bitte den Geldbetrag in Euro angeben:
150
Bitte die Laufzeit in Jahren angeben:
3
Bitte die Zinsen angeben (5% = 0.05):
0.04
Wert nach Jahr 1: 156.0
Wert nach Jahr 2: 162.24
Wert nach Jahr 3: 168.7296


Ausgabe bei falscher Eingabe:

Konsolenausgabe:


Bitte den Geldbetrag in Euro angeben:
200t
Eingabe ist keine Zahl!
Bitte den Geldbetrag in Euro angeben:
vote_ok
von jamosch (170 Punkte) - 08.05.2017 um 16:54 Uhr
Quellcode ausblenden Java-Code
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;

public class zinsBerechnen {
public static void main(String[] args) throws IOException {
	float ergebnis;
	InputStreamReader isr = new InputStreamReader(System.in);
	BufferedReader bf = new BufferedReader(isr);
	System.out.println("Geldbetrag in Euro: ");
	String in = bf.readLine();
	float geldbetrag = Float.parseFloat(in);
	float orggeldbetrag = geldbetrag;

	System.out.println("Jahreszins (0.05 = 5%): ");
	in = bf.readLine();
	float zins = Float.parseFloat(in);	
	
	System.out.println("Laufzeit in Jahren: ");
	in = bf.readLine();
	int laufzeit = Integer.parseInt(in);
	DecimalFormat df = new DecimalFormat("#0.00");

	

	for (int i = 1; i <= laufzeit; i++) {
		ergebnis = geldbetrag+geldbetrag*zins;
		if (i==1)
			System.out.println("Wert nach "+i+" Jahr: "+df.format(ergebnis)+" Euro");
		else 
			System.out.println("Wert nach "+i+" Jahren: "+df.format(ergebnis)+" Euro");
		geldbetrag = ergebnis;
	}
	
}
}
vote_ok
von Zitzus (80 Punkte) - 12.05.2017 um 19:58 Uhr
Quellcode ausblenden Java-Code
import java.util.InputMismatchException;
import java.util.Locale;
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        final Scanner in = new Scanner(System.in);
        in.useLocale(Locale.US);
        double betrag = 0;
        double zins = 0;
        int laufzeit = 0;

        boolean again = false;

        do
        {
            try
            {
                System.out.print("Geldbetrag in Euro: ");
                betrag = in.nextDouble();
                again = false;
            }
            catch(InputMismatchException e)
            {
                again = true;
                in.nextLine();
            }
        }
        while(again);

        do
        {
            try
            {
                System.out.print("Jahreszins (0.05 = 5%): ");
                zins = in.nextDouble();
                again = false;
            }
            catch(InputMismatchException e)
            {
                again = true;
                in.nextLine();
            }
        }
        while(again);

        do
        {
            try
            {
                System.out.print("Laufzeit in Jahren: ");
                laufzeit = in.nextInt();
                again = false;
            }
            catch (InputMismatchException e)
            {
                again = true;
                in.nextLine();
            }
        }
        while(again);

        for(int i = 1; i <= laufzeit; i++)
        {
            double result = betrag * Math.pow((1 + zins), i);
            if(i == 1)
            {
                System.out.println("Wert nach " + i + " Jahr: " +result);
            }
            else
            {
                System.out.println("Wert nach " + i + " Jahren: " +result);
            }
        }
    }
}
vote_ok
von guandi (230 Punkte) - 03.08.2018 um 12:27 Uhr
Die Ausgabe entspricht jetzt nicht den Vorgaben aber ansonsten würde ich sagen, dass das Programm gelungen ist :)

Quellcode ausblenden Java-Code
import java.math.*;
import java.util.Scanner;

public class App {
	

	public static void main(String[] args) {
		// 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.

		System.out.println("Willkommen beim Zinseszinsrechner :)");
		
		double geldbetrag = 0;
		double jahreszinssatz = 0.0;
		int laufzeit = 0;
		
		geldbetrag = eingabeBetrag();
		jahreszinssatz = eingabeJahreszinssatz();
		laufzeit = eingabeLaufzeit();
		
		System.out.println();
		System.out.println("-----");
		System.out.println("Alle Eingaben auf einen Blick:");
		System.out.println("Der einbezahlte Geldbetrag: " + geldbetrag + "€");
		System.out.println("Der Jahreszinssatz: " + jahreszinssatz*100 + "%");
		System.out.println("Die Laufzeit: " + laufzeit + " Jahre."); 
		
		System.out.println("---");
		System.out.println("Abrechung:");
		System.out.println("");
		
		berechnung(geldbetrag, jahreszinssatz, laufzeit);
		
		System.exit(1);
		
	}
	
	static double eingabeBetrag() {
		Scanner einlesen = new Scanner(System.in);
		double eingabe = 0;
		
		try {
			System.out.print("Bitte geben Sie den anzulegenden Betrag ein:");
			eingabe = einlesen.nextDouble();
		}catch (Exception e) {
			System.out.println("Bitte nur Ganzzahlen eingeben!");
			eingabe = eingabeBetrag();
		}
	return eingabe;
	}
	
	static double eingabeJahreszinssatz() {
		Scanner einlesen = new Scanner(System.in);
		double eingabe = 0.0;
		try {
			System.out.print("Jahreszins (Bsp: 0.05 = 5%):");
			eingabe = einlesen.nextDouble();
		}catch (Exception e) {
			System.out.println("Hmmm");
		}
	return eingabe;
	}
	
	static int eingabeLaufzeit() {
		Scanner einlesen = new Scanner(System.in);
		int eingabe = 0;
		
		try {
			System.out.print("Eingabe der Laufzeit:");
			eingabe = einlesen.nextInt();
		}catch (Exception e) {
			System.out.println("Bitte nur Ganzzahlen eingeben!");
			eingabe = eingabeLaufzeit();
		}
	return eingabe;
	}
	
	static void berechnung(double geldbetrag, double jahreszinssatz, int laufzeit) {
		double zinsen;
		
		for(int i=0; i<laufzeit; i++) {
			int j;
			j=i;
			j++;
			zinsen = geldbetrag * jahreszinssatz;
		
			System.out.println(gerundet(zinsen) + "€ Zinsen im " + j + ". Jahr.");
			
			geldbetrag = geldbetrag + zinsen;

			System.out.println("Neuer Geldbetrag nach dem " + j + " Jahr: " + gerundet(geldbetrag) + "€.");
			System.out.println("---");
		}
	}
	
	static String gerundet(double x) {
		String zahl = "";
		final java.text.NumberFormat nf = java.text.NumberFormat.getInstance(); 
        nf.setMaximumFractionDigits(2); 
        zahl = nf.format(new BigDecimal(x));
		
		return zahl;
		
	}

}
vote_ok
von nOrdan (1160 Punkte) - 30.05.2019 um 20:31 Uhr
Anmerkung: Ich arbeite mit dem Programm BlueJ

Quellcode ausblenden Java-Code


import javax.swing.JOptionPane;

import java.text.DecimalFormat;

/**
 * Berechnung von Zinseszinsen 
 * 
 * @author (nOrdan) 
 * @version (30.05.2019 20:29)
 */
public class Zinseszins
{
    
    DecimalFormat df = new DecimalFormat("0.00");

    /**
     * Konstruktor für Objekte der Klasse Zinseszins
     */
    public Zinseszins()
    {

    }

    public void zinseszins()
    {
        boolean valid1 = false;
        boolean valid2 = false;
        boolean valid3 = false;
        boolean valid4 = false;
        double geldbetrag = 0;
        double jahreszins = 0;  
        int laufzeit = 0;
        while (valid1 == false)
        {
            String input1 = JOptionPane.showInputDialog("Geben sie ihren Geldbetrag ein");
            try
            {
                if (input1 == null) 
                {
                    System.exit(0); //Drückt der User auf abbrechen wird null zurück gegeben und das Programm wird beendet
                }
                else if (input1.length() == 0)
                {
                    continue; //Gibt der User nichts ein und drückt auf Ok wird die While-Schleife weiter fortgeführt bis der User etwas eingibt und auf Ok drückt
                }
                else
                {
                    geldbetrag = parseDouble(input1);
                    valid1 = true;
                }
            }
            catch(Exception e)
            {
                errorMessage("Invalid input","Error");
            }
        }            
        while (valid2 == false)
        {
            String input2 = JOptionPane.showInputDialog("Geben sie an ob sie ihren Jahreszins in Prozent(p oder %) oder als Dezimalzahl(d) angeben wollen");
            if (input2 == null)                    
            {
                System.exit(0);                        
            }
            else if (input2.length() == 0)
            {
                continue;
            }
            else
            {
                input2 = input2.toLowerCase().replaceAll("\\s+","");
                if (input2.equals("p") || input2.equals("%"))
                {
                    valid2 = true;
                    while (valid3 == false)
                    {
                        input2 = JOptionPane.showInputDialog("Geben sie den Jahreszins in Prozent ein");
                        try
                        {
                            if (input2 == null)
                            {
                                System.exit(0);
                            }
                            else if (input2.length() == 0)
                            {
                                continue;
                            }
                            else
                            {
                                jahreszins = parseDouble(input2);
                                jahreszins /= 100;
                                valid3 = true;
                            }
                        }
                        catch(Exception e)
                        {
                            errorMessage("Invalid input","Error");
                        }
                    }
                }
                else if (input2.equals("d"))
                {                            
                    valid2 = true;
                    while (valid3 == false)
                    {
                        input2 = JOptionPane.showInputDialog("Geben sie den Jahreszins als Dezimalzahl ein");
                        try
                        {
                            if (input2 == null)
                            {
                                System.exit(0);
                            }
                            else if (input2.length() == 0)
                            {
                                continue;
                            }
                            else
                            {
                                jahreszins = parseDouble(input2);
                                valid3 = true;
                            }
                        }
                        catch(Exception e)
                        {
                            errorMessage("Invalid input","Error");
                        }
                    }
                }            
                else
                {
                    errorMessage("Invalid input, please repeat your action","Error");
                    continue;
                }
            }
        }
        while (valid4 == false)
        {
            String input3 = JOptionPane.showInputDialog("Geben sie die Laufzeit ein");
            try 
            {
                if (input3 == null) 
                {
                    System.exit(0); //Drückt der User auf abbrechen wird null zurück gegeben und das Programm wird beendet
                }
                else if (input3.length() == 0)
                {
                    continue; //Gibt der User nichts ein und drückt auf Ok wird die While-Schleife weiter fortgeführt bis der User etwas eingibt und auf Ok drückt
                }
                else
                {
                    laufzeit = parseInt(input3);
                    valid4 = true;                            
                }
            }
            catch(Exception e)
            {
                errorMessage("Invalid input","Error");
            }
        }    
        ergebnisGeld(geldbetrag,jahreszins,laufzeit);
    }            

    private void errorMessage(String message,String errorName)
    {
        JOptionPane.showMessageDialog(null,message,errorName,JOptionPane.ERROR_MESSAGE); 
    }

    private int parseInt(String input)
    {
        return Integer.parseInt(input);
    }

    private double parseDouble(String input)
    {
        return Double.parseDouble(input);
    }

    private void konsoleLeeren()
    {
        System.out.print('\u000C');
    }

    private void ergebnisGeld(double geldbetrag,double jahreszins,int laufzeit)
    { 
        konsoleLeeren();
        for (int i = 1; i < (laufzeit+1); i++)
        {
            geldbetrag = geldbetrag * (1 + jahreszins);
            if (i == 1)
            {
                System.out.println("Geld nach " + i + " Jahr: " + df.format(geldbetrag) + "€");
            }
            else
            {
                System.out.println("Geld nach " + i + " Jahren: " + df.format(geldbetrag)  + "€");
            }
        }
    }
}



vote_ok
von paddlboot (3970 Punkte) - 09.07.2019 um 09:47 Uhr
Quellcode ausblenden Java-Code
import java.util.*;
import java.text.*;

public class Zinseszins {
	public static void main(String[] args) {
		double geldbetrag;
		double jahreszins;
		int laufzeit;
		double erg;
		
		DecimalFormat f = new DecimalFormat("#0.00");
		Scanner scanner = new Scanner(System.in);
		
		System.out.print("Geldbetrag in Euro:\t");
		geldbetrag = scanner.nextInt();
		System.out.print("Jahreszins (0.05 = 5%):\t");		
		jahreszins = scanner.nextDouble();		
		System.out.print("Laufzeit in Jahren:\t");
		laufzeit = scanner.nextInt();
		
//		Berechnung
		for(int i = 0; i < laufzeit; i++)
		{
			if(i == 0)
			{
				System.out.print("Wert nach 1 Jahr:\t");
			}
			else
			{
				System.out.print("Wert nach " + (i+1) + " Jahren:\t");
			}
			
			erg = (geldbetrag * jahreszins) + geldbetrag;
			geldbetrag = erg;
			
			System.out.print(f.format(erg) + " Euro\n");
			
		}
		scanner.close();
	}
}
vote_ok
von Flocke (180 Punkte) - 05.11.2019 um 17:18 Uhr
Quellcode ausblenden Java-Code
import java.util.Scanner;

public class Main {
	public static void main(String argsp[]) {
		
		float geldbetrag;
		float zinssatz;
		int laufzeit;
		
		Scanner s = new Scanner(System.in);
		
		System.out.print("Bitte Geldbetrag eingeben: ");
		geldbetrag = s.nextFloat();
		
		System.out.print("Bitte Zinssatz eingeben (0,05 = 5%): ");
		zinssatz = s.nextFloat();
		
		System.out.print("Bitte Jahreslaufzeit eingeben: ");
		laufzeit = s.nextInt();
		s.close();
		
		for(int i = 0; i < laufzeit; i++) {
			geldbetrag += geldbetrag * zinssatz;
			System.out.println("Wert nach Jahr" + (i+1) + ": " + geldbetrag);
		}
	}
}


Konsolenausgabe:

Bitte Geldbetrag eingeben: 150
Bitte Zinssatz eingeben (0,05 = 5%): 0,04
Bitte Jahreslaufzeit eingeben: 3
Wert nach Jahr1: 156.0
Wert nach Jahr2: 162.24
Wert nach Jahr3: 168.7296
vote_ok
von 0 (0 Punkte) - 21.01.2021 um 20:40 Uhr
Quellcode ausblenden Java-Code
package de.patrick260.trainYourProgrammer.exercise_4;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.NumberFormat;

public class ZinseszinsberechnungUndAusgabe {

    public static void main(String[] args) {

        double geld = 0;
        double zins = 0;
        int laufzeit = 0;

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Geldbetrag in Euro: ");

        try {
            String input = reader.readLine();

            while (!isDouble(input)) {
                System.out.println("Invalid Input!");
                System.out.print("Geldbetrag in Euro: ");
                input = reader.readLine();
            }

            geld = Double.parseDouble(input);
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.print("Jahreszins: ");

        try {
            String input = reader.readLine();

            while (!isDouble(input)) {
                System.out.println("Invalid Input!");
                System.out.print("Jahreszins: ");
                input = reader.readLine();
            }

            zins = Double.parseDouble(input);
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.print("Laufzeit in Jahren: ");

        try {
            String input = reader.readLine();

            while (!isInteger(input)) {
                System.out.println("Invalid Input!");
                System.out.print("Laufzeit in Jahren: ");
                input = reader.readLine();
            }

            laufzeit = Integer.parseInt(input);
        } catch (IOException e) {
            e.printStackTrace();
        }

        for (int i = 0; i < laufzeit; i++) {
            geld += geld * (zins / 100);
            NumberFormat n = NumberFormat.getInstance();
            n.setMaximumFractionDigits(2);
            System.out.println(String.format("Wert nach " + (i + 1) + " Jahren: " + n.format(geld)));
        }

    }

    private static boolean isInteger(String s) {
        try {
            Integer.parseInt(s);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    private static boolean isDouble(String s) {
        try {
            Double.parseDouble(s);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

}
vote_ok
von TheChuggler (120 Punkte) - 18.06.2021 um 15:48 Uhr
Quellcode ausblenden Java-Code
import java.util.Scanner;

class Interest {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        double[] input = {0., 0., 0.};
        String[] inputRequest = new String[3];
        inputRequest[0] = "Amount of money (use a comma (\",\") as a decimal point): ";
        inputRequest[1] = "Interest rate p.a. (0,05 = 5%): ";
        inputRequest[2] = "Number of years: ";
        boolean[] correctInput = {false, false, false};
        for (int i = 0; i < input.length; i++) {
            while (!correctInput[i]) {
                System.out.println(inputRequest[i]);
                try {
                    input[i] = scan.nextDouble();
                    if (input[i] < 0) {
                        System.out.println("Please enter a number > 0!");
                        
                    } else {
                        correctInput[i] = true;
                    }
                } catch (Exception e) {
                    System.out.println("Please enter a number > 0!");
                    scan.next();
                }
            }
        }
        
        for (int i = 1; i < input[2] + 1; i++) {
            input[0] *= (input[1] + 1.) ;
            if (i == 1) {
                System.out.printf("Amount of money after 1 year: %.2f Euro\n", input[0]);
            } else {
                System.out.printf("Amount of money after " + i + " years: %.2f Euro\n", input[0]);
            }
        }            
        scan.close();        
    }
}


Konsolenausgabe:

Amount of money (use a comma (",") as a decimal point):
150
Interest rate p.a. (0,05 = 5%):
0,04
Number of years:
3
Amount of money after 1 year: 156,00 Euro
Amount of money after 2 years: 162,24 Euro
Amount of money after 3 years: 168,73 Euro
1810932

Du scheinst einen AdBlocker zu nutzen. Ich würde mich freuen, wenn du ihn auf dieser Seite deaktivierst und dich davon überzeugst, dass die Werbung hier nicht störend ist.