Java >> Tutoriel Java >  >> Java

Séquences d'échappement et spécificateurs de format Exemple en Java

Écrivez un programme Java qui illustre diverses séquences d'échappement et spécificateurs de format comme le retour arrière, la représentation hexadécimale, le retour chariot, l'espace de tabulation et plus encore.

Programme ou solution

				
				
					

class Program

{

public static void main(String args[])

{

                // Escape Sequences

// \b back space deletes the previous character

System.out.printf("Hello\b World\n");

 

        
                // \n New line moves cursor to new line before printing World

System.out.printf("Hello\nWorld\n");

    
                // \t tab space leaves a tab space before World

System.out.printf("Hello\t World\n"); 

// \r Carriage Return Moves cursor to starting position in the same line so Devil will be Over Written

System.out.printf("Hello World \rDevil\n"); 

// \" prints double Quotes

System.out.printf("\"Decode School\"\n");


// \\ Prints slash, so a\b 

System.out.printf("a\\b\n");

                

                // Format Specifiers

// .2f   two digit precision in floating point numbers 

System.out.printf("%.2f\n",10/3.0); 

// %x hexa representation (a for 10)

System.out.printf("%x\n",10); 

// %o Octal representation (12 for 10)

System.out.printf("%o\n",10); 

// %b Boolean representation

System.out.printf("%b\n",10); 

}

}

Sortie

Explication du programme

La séquence d'échappement commence par \

Le spécificateur de format commence par %



Balise Java