有关Prime numbers输出的问题
The following pseudocode will build an array, a, containing the first n prime numbers: Set the first element of a to the lowest prime number (2).
For each odd number, test, starting at 3:
For each element, p, in a:
Compute the remainder of test/p.
If the remainder was non-zero for every p:
Append test to a.
If the length of a equals n:
break
Assignment
Create a Java program, Prime.java, to implement this algorithm. The program should accept one command-line argument, the number of primes to generate (n). It should respond by printing the first n prime numbers, followed by a message stating the n-th prime number. If more than 10 prime numbers are requested, the program should print only the first five and the last five, separated by a line displaying an ellipsis ("...").
For example:
$ java Prime 13
2
3
5
7
11
...
23
29
31
37
41
The 13-th prime number is 41.
$
Other requirements:
? Your program should generate the entire array of prime numbers before printing any of them, rather than printing them "on the fly".
? For n equal to 1, 2, or 3, the output should read "first", "second", or "third" rather than "1-th", "2-th", or "3-th". (Feel free to generalize this to other numbers.)
? If no command-line argument is supplied, the program should print a helpful message to the Java error stream, System.err, and exit.
? Use int (4-byte) variables throughout the program.
? Your program should make no assumption about the maximum number of primes which can be requested. For example, you cannot simply define a as an 8000-element array.
? If the command-line argument is less than one, assume that it is equal to one.
下面的代码是我写的,只是能完成生成符合要求的素数....
现在要实现控制台输入的,我对类型转换的地方还不熟,请大家帮帮忙...
/**
* @(#)Prime.java
*
*
* @author liude
* @version 1.00 2008/3/12
*/
public class Prime {
/**
* Creates a new instance of <code>Prime</code>.
*/
public Prime() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int input;int i,j;input=568;//input就是接收输入的参数,并转换为int类型
int temp=3;
int []prime=new int[input];
prime[0]=2;
if(input>1)
prime[1]=3;
for(i=2;i<input;i++)
{j=0;
while(i!=j)
{if(temp%prime[j]==0)
{temp=temp+2;
j=0;
}
else
j++;
}
prime[i]=temp;
j=0;
}
for(i=0;i<input;i++)
System.out.println(prime[i]);
}
}