2008年12月12日 星期五

simple java(十一)---application(10)

Write programs for the below output: (hint: using nested for)

(a)
1
2
3
1
2
3
1
2
3

solution:

public class range
{
public static void main (String args[])
{

for(int i=1;i<=3;i++){
for(int j=1;j<=3;j++){

System.out.println( j);}
}
}}
(b)
12345
12345
12345
12345
12345
solution:
public class range {

public static void main (String args[])
{

for(int i=1;i<=5;i++){for(int j=1;j<=5;j++){

System.out.print( j);}

System.out.print("\n");}}}
(c)
1
12
123
1234
12345
solution:

public class range {

public static void main (String args[])
{
for(int j = 1;j<=5;j++) {
for(int i=1;i<=j;i++)
System.out.print(i);
System.out.println(); }
System.out.println(); }}

(d)
55551
55512
55123
51234
12345
solution:
public class range {

public static void main (String args[]){
for (int i=1;i<=5;i++){
for(int j=i;j<=4;j++)
System.out.print("5");

for(int k=1;k<=i;k++)
System.out.print(k);

System.out.println();}}}

沒有留言: