English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
所有分類
0

請問怎麼用java的for迴圈 列印出以下的畫面

1
22
333
4444
55555

另外一個是算出1~100內的質數 ?


ps 這個不是功課,想了好久,還是想不出來的豬頭留 一 一"

2007-01-29 00:44:42 · 2 個解答 · 發問者 Anonymous in 電腦與網際網路 程式設計

ωετμοφντ 非常感謝您
不過,我 run 第二個程式時
跑出來一個非質數 4 耶 !

2007-01-30 12:23:36 · update #1

2 個解答

請參考我的做法

public class A {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 0; j < i; j++) {
System.out.print(i);
}
System.out.println();
}
}
}
public class B {
public static void main(String[] args) {
for (int i = 2; i <= 100; i++) {
boolean isPrime = true;
for (int j = 2; j < i/2; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) System.out.println(i);
}
}
}

2007-01-29 06:22:02 · answer #1 · answered by ? 7 · 0 0

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

public class B {
public static void main(String[] args) {
for (int i = 2; i <= 100; i++) {
boolean isPrime = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) System.out.println(i);
}
}
}

2007-02-07 06:38:09 補充:
將樓上的做個小修正,解答贈點請給樓上

2007-02-07 01:36:40 · answer #2 · answered by jonathan 4 · 0 0

fedest.com, questions and answers