Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1eb1ba5

Browse files
author
Basma Ramadan+
authored
Update Exercise_05_45.java
1 parent1fcbde2 commit1eb1ba5

File tree

1 file changed

+37
-25
lines changed

1 file changed

+37
-25
lines changed

‎Exercise_05/Exercise_05_45/Exercise_05_45.java

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,42 @@
88
Write a program that prompts the user to enter ten numbers, and displays the
99
mean and standard deviations of these numbers using the following formula:
1010
*/
11-
importjava.util.Scanner;
1211

13-
publicclassExercise_05_45 {
14-
publicstaticvoidmain(String[]args) {
15-
Scannerinput =newScanner(System.in);
16-
doublemean,// Holds the mean
17-
deviation,// Holds the standard deviation
18-
number;// Holds user input
19-
mean =deviation =0;// Set mean and deviation to 0
20-
21-
// Prompt the user to enter ten numbers
12+
importjava.util.Scanner;
13+
publicclassExercises5 {
14+
publicstaticvoidmain(String[]args) {
15+
Scannerinput =newScanner(System.in);
16+
double []numbers =newdouble [10] ;
2217
System.out.print("Enter ten numbers: ");
23-
24-
// Compute mean and standard deviation
25-
for (inti =1;i <=10;i++) {
26-
number =input.nextDouble();
27-
mean +=number;
28-
deviation +=Math.pow(number,2);
29-
}
30-
deviation =Math.sqrt((deviation - (Math.pow(mean,2) /10)) / (10 -1));
31-
mean /=10;
32-
33-
// Display results
34-
System.out.println("The mean is " +mean);
35-
System.out.printf("The standard deviation is %.5f\n",deviation);
36-
}
37-
}
18+
for (inti =0;i <10;i++)
19+
numbers[i] =input.nextDouble();
20+
21+
doublemean,deviation;
22+
23+
mean =mean(numbers);
24+
deviation =std(numbers,mean);
25+
26+
System.out.println("The mean is " +mean);
27+
System.out.printf("The standard deviation is %.5f\n",deviation);
28+
29+
}
30+
publicstaticdoublemean(doublenumArray[]){
31+
doublesum =0.0;
32+
intlength =numArray.length;
33+
for(doublenum :numArray)
34+
sum +=num;
35+
36+
doublemean =sum/length;
37+
38+
returnmean;
39+
}
40+
41+
publicstaticdoublestd(doublenumArray[] ,doublemean{
42+
doublestandardDeviation =0.0;
43+
intlength =numArray.length;
44+
for(doublenum:numArray) {
45+
standardDeviation +=Math.pow(num -mean,2);
46+
}
47+
returnMath.sqrt(standardDeviation /(length -1));
48+
}
49+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp