引用 | 編輯
totsi
2009-04-22 08:22 |
3樓
▲ ▼ |
Java我不會..不過用c++可以這樣寫
char *pcNum = NULL; int nNo = 0; scanf("%d", &nNo);//取得輸入值 pcNum = new char[nNo];//allocate memory for ( int i = 0 ; i < nNo ; i++ ) { pcNum[0] = i+1;//放在陣列裡面 printf("%s\n", pcNum);//印出來 } delete pcNum;//release memory pcNum = NULL; x0 |
引用 | 編輯
jim28791
2009-04-23 23:36 |
4樓
▲ |
不知你想要的是不是這樣~~
複製程式 import java.io.*; class bn { public static void main(String args[]) throws IOException { BufferedReader buf; String str; int x,y=0; buf = new BufferedReader(new InputStreamReader(System.in)); System.out.print("請輸入數字:"); str = buf.readLine(); x=Integer.parseInt(str); int tmn[]=new int[x]; for(int i=1;i<x;i++)if(test(i)==0) { tmn[y]=i; y++; } for(int i=0;i<tmn.length;i++)if(tmn[i]!=0)System.out.println(tmn[i]);else break; } public static int test(int n) { for(int i=2;i<=Math.sqrt(n);i++)if(n%i==0)return 1; return 0; } } java還不太熟~~~ 對於怎麼讓陣列需要時才加空間~ 這我不太會~~ 所以~這程式會佔用許多多餘的空間XD x0 |