理解这个任务'总结'

时间:2013-11-09 19:33:00

标签: java sum

我在理解这项任务要求我做的事情上遇到了很多麻烦。我是一个绝对的初学者,一般学习Java /编程几周,这对我来说非常困难。因此,对于标题中没有特别具体而道歉。

如果有人可以向我解释,我真的很感激,或许可以通过一些示例代码示例(我不是要求人们为我做任务,我只是觉得这样更容易理解)所以我可以知道该怎么做。

谢谢。

总结规范:

此程序从命令行获取输入。

The first input is K, an integer, followed by an arbitrary number a_1, ..., a_N of floating-point numbers.

If K=0, then N is output.

If K > 0, then the a_i are grouped in groups of size K, inside the groups the numbers are multiplied, and all the products are added, yielding the output.
If K < 0, then the summation sums up the reciprocal values of the products.
In the special case K=1 the output thus is a_1 + ... + a_N.
In the case K=-1 the output is 1/a_1 + ... + 1/a_N.

There are two error cases:
If no command-line argument is given, then error-code 1 is to be returned.
If K < 0, and one of a_i = 0, then error-code 2 is to be returned.
In both cases there must be no output.
The return-code of a program is set via System.exit(code);

请注意,程序的返回代码是通过echo $?在命令行中获得的。 绝不能有任何其他输出。也不允许额外的空格或换行符。

以下是没有错误的示例:http://pastebin.com/F2uz262v

1 个答案:

答案 0 :(得分:0)

您可以在此处找到命令行参数:

public static void main(String args[]) {
                            // ^ these are the command line arguments

所以要获得K,

int K = Integer.parseInt(args[0]);

但实际上并不是你应该如何编写它,因为这是Java和Java变量应该以小写字母开头。

int k = Integer.parseInt(args[0]);

首先让我们来处理k = 0的情况。我们必须测试k是否为0:

if(k == 0) {

如果是,则N是附加参数的数量,即

  int numberOfAdditionalArguments = args.length - 1;

然后我们打印它(之后没有换行符,就像它说的那样!)

  System.out.print(numberOfAdditionalArguments);

然后我们关闭} block

}

我认为这足以让你开始。