0

この課題が私に求めていることを理解するのに非常に苦労しています。私は完全な初心者で、Java/プログラミング全般を数週間学習していますが、これは私にとって非常に困難です。そのため、タイトルがあまり具体的でなくて申し訳ありません。

誰かが私に説明してくれると本当にありがたいです.1つの例のサンプルコードを少し付けてください.何をすべきかを考えます。

ありがとうございました。

加算の仕様:

このプログラムは、コマンド ラインから入力を取得します。

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

4

1 に答える 1

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);

次に、} ブロックを閉じます

}

始めるにはそれで十分だと思います。

于 2013-11-09T20:36:49.587 に答える