1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
| /*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package losange;
import java.util.Scanner;
/**
*
* @author Samsung
*/
public class Losange {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int nombreLigne = 0;
String caractereAAfficher ="";
int videExt = 0, videInter = 2;
System.out.println("donner le caractère à afficher puis cliquer sur entrer :");
Scanner clavier = new Scanner(System.in);
caractereAAfficher = clavier.nextLine();
//System.out.println("caratère = " + caractereAAfficher);
System.out.println("donner le nombre de ligne puis cliquer sur entrer :");
nombreLigne = clavier.nextInt();
clavier.nextLine();
//System.out.println("nombre de ligne = " + nombreLigne);
videExt = nombreLigne / 2;
if (nombreLigne % 2 == 0)
videExt--;
for ( int i = 0; i<videExt; i++)
System.out.print(" ");
System.out.print(caractereAAfficher + caractereAAfficher );
System.out.println();
int ligneparcouru = videExt;
if (nombreLigne % 2 != 0) {// si nombre de ligne est impair
for(int i =0; i < ligneparcouru; i++) { // dessine la partie haute du losange
videExt--;
for(int j =0; j < videExt ;j++)
System.out.print(" ");
System.out.print(caractereAAfficher);
for( int k = 0; k < videInter ; k++ )
System.out.print(" ");
System.out.print(caractereAAfficher);
videInter = videInter + 2;
System.out.println();
}
} else {//si nombre de ligne est paire
int j =0,k=0;
for(int i =0; i < ligneparcouru; i++) { // dessine la partie haute du losange
videExt--;
for( j =0; j < videExt ;j++)
System.out.print(" ");
System.out.print(caractereAAfficher);
for( k = 0; k < videInter ; k++ )
System.out.print(" ");
System.out.print(caractereAAfficher);
videInter = videInter + 2;
System.out.println();
}
videInter = videInter - 2;
for( j =0; j < videExt ;j++)
System.out.print(" ");
System.out.print(caractereAAfficher);
for( k = 0; k < videInter ; k++ )
System.out.print(" ");
System.out.print(caractereAAfficher);
System.out.println();
}
if (nombreLigne % 2 == 0) {// si nombre de ligne est paire
for(int i =0; i < ligneparcouru; i++) { // dessine la partie bas du losange
videExt++;
videInter = videInter - 2;
for(int j =0; j < videExt ;j++)
System.out.print(" ");
System.out.print(caractereAAfficher);
for( int k = 0; k < videInter ; k++ )
System.out.print(" ");
System.out.print(caractereAAfficher);
System.out.println();
}
}else{// si nombre de ligne est impair
videInter = videInter - 2;
for(int i =0; i < ligneparcouru; i++) { // dessine la partie bas du losange
videExt++;
videInter = videInter - 2;
for(int j =0; j < videExt ;j++)
System.out.print(" ");
System.out.print(caractereAAfficher);
for( int k = 0; k < videInter ; k++ )
System.out.print(" ");
System.out.print(caractereAAfficher);
System.out.println();
}
}
}
} |