Dibawah ini merupakan contoh class looping bersarang.
public class LoopingBersarang
{
public static void main(String[] args) {
String text1="STMIK BANI SALEH";
String text2="BAHASA PEMROGRAMAN 2";
int a,b;
for(a=1; a<=2; a++)
{
System.out.println(" "+text1);
for(b=1; b<=3; b++)
{
System.out.println(" "+text2);
}
System.out.println(" ");
}
}
}
{
public static void main(String[] args) {
String text1="STMIK BANI SALEH";
String text2="BAHASA PEMROGRAMAN 2";
int a,b;
for(a=1; a<=2; a++)
{
System.out.println(" "+text1);
for(b=1; b<=3; b++)
{
System.out.println(" "+text2);
}
System.out.println(" ");
}
}
}
Output :
public class LoopingBersarang1
{
public static void main(String[] args) {
int[] data1={0,1,2,3,4,5};
int[] data2={0,1,2,3,4,5};
int jum,i,j;
System.out.println(" + 0 1 2 3 4 5");
System.out.println(" -------------");
for(i=0; i<=5; i++)
{
System.out.print(" "+i);
for(j=0; j<=5; j++)
{
jum=data1[i]+data2[j];
System.out.print(" "+jum);
}
System.out.println(" ");
}
}
}
{
public static void main(String[] args) {
int[] data1={0,1,2,3,4,5};
int[] data2={0,1,2,3,4,5};
int jum,i,j;
System.out.println(" + 0 1 2 3 4 5");
System.out.println(" -------------");
for(i=0; i<=5; i++)
{
System.out.print(" "+i);
for(j=0; j<=5; j++)
{
jum=data1[i]+data2[j];
System.out.print(" "+jum);
}
System.out.println(" ");
}
}
}
Output :
public class LoopingBersarang2
{
public static void main(String[] args) {
int[] data1={2,4,6,8};
int[] data2={2,4,6,8};
int jum,i,j,x=0;
System.out.println(" + 2 4 6 8");
System.out.println(" ---------------------");
for(i=0; i<=3; i++)
{
x=x+2;
System.out.print(" "+x);
for(j=0; j<=3; j++)
{
jum=data1[i]+data2[j];
System.out.print(" "+jum);
}
System.out.println("");
}
}
}
{
public static void main(String[] args) {
int[] data1={2,4,6,8};
int[] data2={2,4,6,8};
int jum,i,j,x=0;
System.out.println(" + 2 4 6 8");
System.out.println(" ---------------------");
for(i=0; i<=3; i++)
{
x=x+2;
System.out.print(" "+x);
for(j=0; j<=3; j++)
{
jum=data1[i]+data2[j];
System.out.print(" "+jum);
}
System.out.println("");
}
}
}
Output :
0 komentar:
Posting Komentar