-2

So i'd like to align the row that displays the tenth year with rest of the numbers. I've tried printf but it doesn't seem to work. I've looked at many posts already but everything seems to be roaming around printf with some string formats. If you can juste explain how to do it to me with some examples it would help me a lot. Thanks for the help.

public class Foo {

    public static void main(String[] args) {
        double capitalDeDepart = capitalDepart(0);
        double tauxInteretAnnuel = interetAnnuel(0);
        double anneeTotalPlacement = dureePlacement(0);
        affichage();
        calcul(capitalDeDepart, tauxInteretAnnuel, anneeTotalPlacement);
    }

    public static double capitalDepart(double capitalDeDepart) {
        Scanner clavier = new Scanner(System.in);
        System.out.print("Indiquez le capital de départ : ");
        capitalDeDepart = clavier.nextDouble();
        return capitalDeDepart;
    }

    public static double interetAnnuel(double tauxInteretAnnuel) {
        Scanner clavier = new Scanner(System.in);
        System.out.print("Inscrivez le taux d'intérêt annuel : ");
        tauxInteretAnnuel = clavier.nextDouble();
        return tauxInteretAnnuel;
    }

    public static double dureePlacement(double anneeTotalPlacement) {
        Scanner clavier = new Scanner(System.in);
        System.out.print("Indiquez la durée du placement en années : ");
        anneeTotalPlacement = clavier.nextDouble();
        return anneeTotalPlacement;
    }

    public static void affichage() {
        System.out.println("Année    Capital    Intérêt    Nouveau capital");
        System.out.println("----------------------------------------------");
    }

    public static void calcul(double capitalDeDepart, double tauxInteretAnnuel, double anneeTotalPlacement) {
        int annee = 0;
        double interet = 0;
        double nouveauCapital = 0;
        double tauxEnDecimal = 0;

        do {
            annee++;
            capitalDeDepart = capitalDeDepart + interet;
            tauxEnDecimal = tauxInteretAnnuel / 100;
            interet = capitalDeDepart * tauxEnDecimal;
            nouveauCapital = (int)(capitalDeDepart + interet);
            System.out.println("    " + annee + "     " + (int)capitalDeDepart + "$      " + (int)interet + "$             " + (int)nouveauCapital + "$");

        } while (annee != anneeTotalPlacement);
    }
}

Indiquez le capital de départ : 10000 Inscrivez le taux d'intérêt annuel : 10 Indiquez la durée du placement en années :

Année    Capital    Intérêt    Nouveau capital
----------------------------------------------
    1     10000$      1000$             11000$
    2     11000$      1100$             12100$
    3     12100$      1210$             13310$
    4     13310$      1331$             14641$
    5     14641$      1464$             16105$
    6     16105$      1610$             17715$
    7     17715$      1771$             19487$
    8     19487$      1948$             21435$
    9     21435$      2143$             23579$
    10     23579$      2357$             25937$
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
  • possibly related: [How can I create table using ASCII in a console?](https://stackoverflow.com/q/15215326) – Pshemo Nov 05 '20 at 21:50
  • 1
    printf is how. If it doesn't seem to work, write it that way, show your code, show the output, and show the output you wanted it to print. Generally, if all answers say 'X is the way', you try it, and it doesn't do what you want, the usual strategy is _NOT_ to just abandon the plan entirely immediately. – rzwitserloot Nov 05 '20 at 21:56
  • You need to write-justify the first column. This usually used printf. – NomadMaker Nov 06 '20 at 00:39

2 Answers2

1

Just replase your System.out.println(...) with following:

System.out.format("%2d     %5d$      %5d$             %5d$\n", annee,
                  (int)capitalDeDepart, (int)interet, (int)nouveauCapital);
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
0

you must will try add a String variable... like...

String reglerAnnee;
...
// add a space if year(anne) is less than 10
// using a ternary operator, like an "if" but less code
reglerAnnee = annee < 10 ? " " : "";
..
// and add to the println ...
System.out.println("    "+reglerAnnee + annee + "     " + (int) capitalDeDepart 
                 + "$      " + (int) interet + "$             " 
                 + (int) nouveauCapital + "$");
...

The result will be like:

 1     10000$      1000$             11000$
 2     11000$      1100$             12100$
...
 9     21435$      2143$             23579$
10     23579$      2357$             25937$
Pshemo
  • 122,468
  • 25
  • 185
  • 269