了解しました。この割り当てでは、文字列入力だけでなく、可変数の入力を持つメソッドが必要になります。入力はすべてスキャナーの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キーを押していない場合。これは私が今まで持っているものですが、ここからどこに行くべきかわかりません。連続した値を要求するように取得できますが、すべてが同じ行にあるわけではありません
また、メソッドのリターンとして何を入力するのか知りたいですか?Javaを初めて使用する場合は、助けてください
これは、1行でコードを読み取らずにコードを計算する私のプログラムですが、別々の行でコードを読み取るために必要なのは、同じ行でコードを読み取る方法だけです。
System.out.println("Please enter the number of courses that you would like to\n" + "calculate the Average Score, the Minimum Score and\n" +"the Maximum Score: "); // prompt the user to enter the number of courses for which the average, min and max neeed to be calculated.
//create a scanner object to read from the keyboard
Scanner input = new Scanner(System.in); //declares a new scanner called input to take input from keyboard
int count = input.nextInt(); // read the user input and store it in the count variable
input.nextLine(); //read the end of the line and throw it away
for (int i = 0; i < count; i++)
{
System.out.println("Please enter the name of the course: "); // prompt user to enter course name
String courseName = input.nextLine(); // stores user input from keyboard into variable courseName
System.out.print("Please enter a score for "+ courseName + " or type -1 to indicate\n" + "that there is no more score for this course: "); // prompt the user to enter a grade in the class
double score = input.nextDouble(); // stores user input for score into variable double
int numberOfScores = 0; // declare integer variable called numberofscored and set value to 0
double total = 0; // declare a double variable called total and set value to 0
double min = 100; // declare double variable called min and set value to 100
double max = 0 ; // declare double called max and set value to 0
while (score!=-1) // start while loop for when score does not equal -1
{
if ( score < min) // start of if statement
{ min = score;
} //end of if //start of if statement
if ( score > max)
{ max = score;
} // end of if statement
total = total + score; // sets the value of variable total equal to itself + score
numberOfScores++; // adds 1 to the variable numberofscores
System.out.print("Please enter a score for "+ courseName + " or type -1 to indicate\n" + "that there is no more score for this course: "); // prompt the user to enter a grade in the class
score = input.nextDouble(); // sets the value of score to the next input double from user
}
double average = total/numberOfScores; // declare variable average and set value to total divided buy the number of scores
System.out.println("The Course Name: " + courseName); //print a messege that shows the user the course name he entered
System.out.println("Number Of Scores: " + numberOfScores); // prints a messege with the number of scores
System.out.printf(" The average score: %.2f" , average); // prints a messege with the average score with only 2 decimal places
System.out.println(); // goes to next line
System.out.printf(" The Minimum score: %.2f" , min);// prints message with minimum score with only 2 decimal places
System.out.println(); // goes to next line
System.out.printf(" The Maximum score: %.2f" , max); // prints messege with maximum score with only 2 decimal places
System.out.println();// goes to next line
input.nextLine(); //read the end of the line and throw it away
}
このコードは仕事をしますあなたが混乱していると感じたら私に尋ねてください:
Scanner input = new Scanner(System.in);
LinkedList<Integer> list = new LinkedList<Integer>();
System.out.println("Please enter the name of the course:");
String course=input.next();
System.out.println("Please enter the scores for CSC 201 on a single line and type a -1 at the end");
int max=0;
int min=Integer.MAX_VALUE;
int sum=0;
int grade;
that: while(true){
grade=input.nextInt();
if(grade==-1)
break that;
list.add(grade);
sum=sum+grade;
if(grade>max)
max=grade;
if(grade<min)
min=grade;
}
System.out.println("The course name: "+ course);
System.out.println("Number of Scores :"+list.size());
System.out.println("The Average Score :"+(double)sum/list.size());
System.out.println("The Minimum Score: "+min);
System.out.println("The Maximum Score :"+max);
PS:Integer.MAX_VALUEは巨大な値であり、グレードの制限が100の場合は100が適切かもしれません。
リンクリストを使用する理由
値を1つずつ保存するのは簡単だからです。
いつでも好きなときにすべての要素に到達できます。
また、このリストのメソッドサイズは要素の数であるため、グレード数をカウントするためのカウンターは必要ありません。
また、すべての成績がこのリストに保存されるようになったため、必要に応じて後で印刷することもできます。
次の何かでこれらの成績が必要ない場合は、リストを無視して、それなしでそれを行うことができます。
がんばろう 。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加