Dsini saya akan membantu anda yang mempunyai tugas untuk membuat segitiga pada JAVA .
1. Pembuatan Segitiga Sama Kaki Terbalik dan Segitiga Siku-siku dengan menggunakan JAVA !
Source Code :
import java.io.*;
public class Segitiga
{
public static String bacaInputan()
{
String baca="";
InputStreamReader input = new InputStreamReader
(System.in);
BufferedReader buf = new BufferedReader (input);
try{
baca = buf.readLine();
}catch( IOException e )
{
System.out.println("Error !");
}
return baca;
}
public static void main(String[] args)
{
//digunakan untuk menangkap exception.
int a, b, c, n1, n2, pilih ;
do
{
System.out.println("+|--------------------------------------|+");
System.out.println("+| |+");
System.out.println("+| S
E G I T I
G A |+");
System.out.println("+|
|+");
System.out.println("+----------------------------------------+");
System.out.println("+| MENU |+");
System.out.println("+| 1. Segitiga Sama Sisi Terbalik |+");
System.out.println("+| 2. Segitiga Siku-siku |+");
System.out.println("+| 3. Keluar |+");
System.out.println("+|--------------------------------------|+");
System.out.print(" PILIHAN :");
pilih=Integer.parseInt(bacaInputan());
switch(pilih)
{
case 1:
System.out.println("+----------------------------------------+");
System.out.println("+| |+");
System.out.println("+| S
E G I T I
G A |+");
System.out.println("+|
|+");
System.out.println("+| S
A M A S I
S I |+");
System.out.println("+|
|+");
System.out.println("+----------------------------------------+");
System.out.print(" Masukkan tinggi segitiga :
");
n1=Integer.parseInt(bacaInputan());
System.out.println("");
for(a=1;a<=n1;a++)
{
for(b=1;b<=a;b++)
{
System.out.print(" ");
}
for(c=0;c>=(a-n1);c--)
{
System.out.print("*");
}
for(c=0;c<(n1-a);c++)
{
System.out.print("*");
}
System.out.println();
}
System.out.println("");
System.out.println("Membuat segitiga sama sisi,
sisi="+n1);
break;
case 2:
System.out.println("+----------------------------------------+");
System.out.println("+|
|+");
System.out.println("+| S
E G I T I
G A |+");
System.out.println("+| |+");
System.out.println("+| S
I K U S I
K U |+");
System.out.println("+|
|+");
System.out.println("+----------------------------------------+");
System.out.print(" Masukkan tinggi segitiga :
");
n2=Integer.parseInt(bacaInputan());
System.out.println("");
for(int i=1;i<=n2;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print("*");
}
System.out.println();
}
System.out.println("");
System.out.println("Membuat segitiga siku-siku
dengan sama kaki, alas="+n2+", dan tinggi="+n2);
System.out.println("");
break;
case 3:
System.exit(0);
break;
default: System.out.println("SALAH
INPUT");
break;
}
} while(true);
}
}
Output :
0 komentar
Posting Komentar