for - 請問 java 這兩題該如何解?

3 views
Skip to first unread message

q738...@yahoo.com.tw

unread,
Dec 31, 2010, 10:15:52 PM12/31/10
to EnJoyJava
import java.util.*;

public class example {

public static void main (String[] arg) {

int number = 1;

for (int i=0; i<21; i++)
number *= 2;

System.out.println("第二十個數是"+number);

///////////////以上為第一題//////////////////

Scanner in = new Scanner(System.in);

System.out.println("請輸入一個整數");

int key = in.nextInt();

int count = 0 , number2 = 1;

System.out.println(key+"的因數有以下:");

while (number2 < key) {

if ((key % number2) == 0) {
System.out.print(number2+" ");
count++;
}
number2++;
}

System.out.println("\t共有"+count+"個");

}
}
Reply all
Reply to author
Forward
0 new messages