---由小到大排列


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

}

 

--Console

*
**
***
****
*****

---由大到小排列


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

for(int i=5;i>=1;i--){
for(int j=1;j<=i;j++){
System.out.print("*");
}
System.out.println();
}
}

}

--Console

 

*****
****
***
**
*

 

---

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

for(int i=1;i<=5;i++){
for(int j=4;j>=i;j--){ //第一列有四個$,i從1開始,4到1剛好有四個
System.out.print("$");
} //$的符號印完之後要印出*

for(int j=1;j<=2*i-1;j++){
System.out.print("*");
}
System.out.println();
}
}
}

--Console

$$$$*
$$$***
$$*****
$*******
*********

 

---自行練習


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


for(int i=1;i<=5;i++){
for(int j=4;j>=i;j--){ //第一列有四個$,i從1開始,4到1剛好有四個
System.out.print("$");
} //$的符號印完之後要印出*

for(int j=1;j<=2*i-1;j++){
System.out.print("*");
}

for(int j=4;j>=i;j--){ //第一列有四個$,i從1開始,4到1剛好有四個
System.out.print("$");
} //$的符號印完之後要印出*

System.out.println();
}
}
}

--Console

$$$$*$$$$
$$$***$$$
$$*****$$
$*******$
*********

------------------------------

 

import java.io.*;
import java.util.Scanner;

public class te0604 {
public static void main(String[] args)throws Exception {
//BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
//int keyin = buf.read(),flag;
Scanner scanner = new Scanner(System.in);
int keyin=scanner.nextInt();int flag;

for (int i = 2; i <= keyin; i++) { //判斷i是否為質數
flag = 1 ; //預設為質數
for (int j = 2; j <i; j++) {
if (i % j == 0) //1÷j餘0則不是質數
{ flag = 0 ;}
}
if(flag ==1){System.out.print(i+",");}
}
}

}

 

--Console

72
2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,

 

 

 

 

 

 

 

 

 

arrow
arrow
    全站熱搜

    玥君 發表在 痞客邦 留言(0) 人氣()