简体   繁体   English

如何在Java中对齐数字

[英]How to align numbers in Java

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.我试过 printf 但它似乎不起作用。 I've looked at many posts already but everything seems to be roaming around printf with some string formats.我已经看过很多帖子了,但似乎所有内容都在使用某些字符串格式在 printf 中漫游。 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 : 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$

Just replase your System.out.println(...) with following:只需将您的System.out.println(...)替换为以下内容:

System.out.format("%2d     %5d$      %5d$             %5d$\n", annee,
                  (int)capitalDeDepart, (int)interet, (int)nouveauCapital);

you must will try add a String variable... like...你必须尝试添加一个 String 变量......比如......

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$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM