-1
votes

Java: Comment empêcher ma fonction d'imprimer une virgule à la fin?

public static void intMethod(int myInt)
    {
        System.out.print("All the numbers lower than " + myInt + " and composed only with digits 1 and/or 3: ");
        
        for(int i=1; i<myInt; i++)
        {
            int num = i;
            while (num > 0) 
            {
                if (num % 10 != 1 && num % 10 != 3)
                    break;
                num /= 10;
            }
            if (num == 0) 
            {
                System.out.print(i);
                if(i<myInt)
                {
                    System.out.print(", ");
                }
            }
        }
    }

0 commentaires

5 Réponses :


0
votes

Je vous suggère d'utiliser un StringJoiner et quelque chose comme

System.out.print("All the numbers lower than "
        + myInt + " and composed only with digits 1 and/or 3: ");
StringJoiner sj = new StringJoiner(", ");
for (int i = 1; i < myInt; i++) {
    int num = i;
    while (num > 0) {
        if (num % 10 != 1 && num % 10 != 3) {
            break;
        }
        num /= 10;
    }
    if (num == 0) {
        sj.add(String.valueOf(i));
    }
}
System.out.println(sj);


0 commentaires

0
votes

Si, dans l'itération actuelle, vous ne savez pas si vous devez imprimer une virgule ou non, vous pouvez essayer de collecter d'abord la chaîne entière dans une variable, supprimer la dernière virgule et l'imprimer.

StringBuilder sb = new StringBuidler();
for ... {
    sb.append(...).append(",");
}
if (sb.length() > 0) {  // if sb is not empty
    sb.deleteCharAt(sb.lastIndexOf(",")); // delete last comma
}
System.out.println(sb.toString());


0 commentaires

0
votes

Vous pouvez utiliser un StringBuilder avec une logique qui n'ajoute qu'une virgule à partir du deuxième numéro correspondant:

All the numbers lower than 50 and composed only with digits 1 and/or 3: 1,3,11,13,31,33

Cela imprime:

public static void intMethod(int myInt) {
    System.out.print("All the numbers lower than " + myInt + " and composed only with digits 1 and/or 3: ");
    
    StringBuilder sb = new StringBuilder();

    for (int i=1; i<myInt; i++) {
        int num = i;
        while (num > 0) { 
            if (num % 10 != 1 && num % 10 != 3)
                break;
            num /= 10;
        }
        if (num == 0) {
            if (sb.length() > 0) sb.append(",");
            sb.append(i);
        }
    }

    System.out.println(sb.toString());
}

intMethod(50);


0 commentaires

0
votes

Vous pouvez corriger votre code existant en sortant la virgule avant le nombre (sauf pour la première fois):

All the numbers lower than 25 and composed only with digits 1 and/or 3: 1, 3, 11, 13

Code complet:

public static void intMethod(int myInt)
{
    System.out.print("All the numbers lower than " + myInt + " and composed only with digits 1 and/or 3: ");
    
    for(int i=1; i<myInt; i++)
    {
        int num = i;
        while (num > 0) 
        {
            if (num % 10 != 1 && num % 10 != 3)
                break;
            num /= 10;
        }
        if (num == 0) 
        {
            if (i>1)
                System.out.print(", ");
            System.out.print(i);
        }
    }
}

Sortie pour intMethod(25); :

if (num == 0) 
{
    if (i>1)
        System.out.print(", ");
    System.out.print(i);
}


0 commentaires

0
votes

Utiliser une méthode utilitaire comme suggéré par Elliott est généralement la meilleure façon de procéder, mais si vous voulez / devez vraiment le faire manuellement en utilisant un booléen, c'est un moyen facile de le faire.

Sur la base de votre premier exemple, j'ai ajouté un booléen pour contrôler l'impression de la virgule:

public static void intMethod(int myInt) {
    boolean printComma = false;   // Don't print the comma the first time around.

    System.out.print("All the numbers lower than " + myInt + " and composed only with digits 1 and/or 3: ");
        
    for(int i=1; i<myInt; i++)
    {
        int num = i;
        while (num > 0) 
        {
            if (num % 10 != 1 && num % 10 != 3)
                break;
            num /= 10;
        }
        if (num == 0) 
        {
            if (printComma) {
                // *After* the first iteration, the printComma will be true
                // so *before* we print the second and subsequent digits
                // we will output the comma that separates this digit from the previous one.
                System.out.print(", ");
            } else {
                // first time through, printComma is false, so we end up here
                // set our flag so that from now on, we will print the comma.
                printComma = true;
            }
            System.out.print(i);
            
        }
    }
}


0 commentaires