0

よし、文字列入力だけでなく、可変数の入力を持つメソッドを必要とするこの割り当てを取得しました。入力はすべてスキャナの 1 行にある必要があり、メソッドは入力された値の数、平均値、最大値、最小値、および入力された文字列を返す必要があります。

これは、ターミナル ウィンドウが次のように表示される例です。

Please enter the name of the course: CourseNameHere
Please enter the scores for CSC 201 on a single line and type a -1 at the end
71 02 81 44 84 17 38 11 20 05 93 -1

The course name     : CourseNameHere
Number of Scores    : 11
The Average Score   : 42.37
The Minimum Score   : 02
The Maximum Score   : 93

平均スコアは小数点以下 2 桁に四捨五入する必要があります (これは処理できると思います)。私にとって唯一の問題は、可変数の入力を 1 行でスキャンすることと、プログラムに入力数をカウントさせる方法です。入力の間にEnterキーを押していない場合。これは私がこれまでに持っているものですが、ここからどこへ行くべきかわかりません。連続した値を要求することはできますが、それらはすべて同じ行にあるわけではありません

public static int calculate(int again)
{
Scanner input = new Scanner(System.in);
int numberOfValues = 0;
int max = -9999;
int min =  100000;
int sum = 0;
double value;

System.out.print("What is the name of the course you wish to evaluate? : ");
String courseName = input.next();
System.out.println("Please enter the scores for the class, press enter between each value." + "\n" + "Enter -1 to finish and calculate.");
for(value = 0; value != 1; numberOfValues++)
        {
            value = input.nextDouble();
            sum += value;





        }
4

3 に答える 3

1

答えは、メソッドへの入力によって異なります。Stringおよび の配列intまたはStringを受け取っていますかString。つまり、ユーザーからの入力はすでに分割されていますか?

例えば...

Scanner kb = new Scanner(System.in);
String courseName = kb.nextLine();
String scoreLine = kb.nextLine();

// courseName and scoreLine now become the parameters to you method...

// You could then use another scanner to break the line down...
Scanner scoreLineScanner = new Scanner(scoreLine);

while (scoreLineScanner.hasNextInt()) {
    int score = scoreLineScanner.nextInt();
    if (score != -1) {
        // Calculate other values...
    } else {
        break;
    }
}

を使用することを考えましString#split(" ")たが、各要素を自分自身に変換する必要がintある可能性があります。これは適切である場合とそうでない場合があります (少し面倒です)。

于 2013-10-04T01:31:45.517 に答える
0

まず最初に arg[0] を使用して、部分文字列を使用して文字列を一連の配列変数に分割する必要があります。次に、array.len または count を使用して残りを行います。この助けを願っています。

于 2013-10-04T01:14:12.510 に答える