有关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]);
}
}
----------------解决方案--------------------------------------------------------
大家帮忙给解决下吧````
----------------解决方案--------------------------------------------------------
弄个输入到n就行了,哪需要什么类型转换啊?
----------------解决方案--------------------------------------------------------
在控制台传入的参数是保存在args[]中的啊...在程序中使用时使用的int类型的数据...我感觉是应该转换的啊```
----------------解决方案--------------------------------------------------------
/**
* @(#)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 i,j;
Integer input=new Integer(args[0]);
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;
}
if(input<4)
{
for(i=0;i<input;i++)
System.out.println("the "+(i+1)+"-th is: "+prime[i]);
}
else if(input>3&&input<11)
{
for(i=0;i<input;i++)
System.out.println("the "+(i+1)+"-th is: "+prime[i]);
}
else
{ for(i=0;i<5;i++)
System.out.println("the "+(i+1)+"-th is: "+prime[i]);
System.out.println("...");
for(i=input-5;i<input;i++)
System.out.println("the "+(i+1)+"-th is: "+prime[i]);
}
}
}
现在基本上完成了...还有点小问题..
就是在这里...
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.)
是不是必须自己手动一个一个转?
----------------解决方案--------------------------------------------------------