SlideShare a Scribd company logo
1 of 4
Download to read offline
// Average.java
import java.util.Scanner;
public class Average{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
double averageMale = 0, averageFemale = 0;
int countMale = 0, countFemale = 0;
char mf;
double average;
System.out.println("How many Students? ");
int n = input.nextInt();
for (int i = 1; i <= n; i++ ) {
System.out.println("Is Student " + i + " male or female? ");
mf = input.next().charAt(0);
if(mf == 'm'){
countMale++;
System.out.println("What is Student's " + i + "'s average? ");
average = input.nextDouble();
averageMale = averageMale + average;
}
else{
countFemale++;
System.out.println("What is Student's " + i + "'s average? ");
average = input.nextDouble();
averageFemale = averageFemale + average;
}
}
System.out.println("The " + countMale + " male Students average is " +
averageMale/countMale);
System.out.println("The " + countFemale + " male Students average is " +
averageFemale/countFemale);
}
}
/*
output:
How many Students?
5
Is Student 1 male or female?
m
What is Student's 1's average?
85
Is Student 2 male or female?
m
What is Student's 2's average?
91
Is Student 3 male or female?
f
What is Student's 3's average?
95
Is Student 4 male or female?
f
What is Student's 4's average?
80
Is Student 5 male or female?
m
What is Student's 5's average?
88
The 3 male Students average is 88.0
The 2 male Students average is 87.5
*/
Solution
// Average.java
import java.util.Scanner;
public class Average{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
double averageMale = 0, averageFemale = 0;
int countMale = 0, countFemale = 0;
char mf;
double average;
System.out.println("How many Students? ");
int n = input.nextInt();
for (int i = 1; i <= n; i++ ) {
System.out.println("Is Student " + i + " male or female? ");
mf = input.next().charAt(0);
if(mf == 'm'){
countMale++;
System.out.println("What is Student's " + i + "'s average? ");
average = input.nextDouble();
averageMale = averageMale + average;
}
else{
countFemale++;
System.out.println("What is Student's " + i + "'s average? ");
average = input.nextDouble();
averageFemale = averageFemale + average;
}
}
System.out.println("The " + countMale + " male Students average is " +
averageMale/countMale);
System.out.println("The " + countFemale + " male Students average is " +
averageFemale/countFemale);
}
}
/*
output:
How many Students?
5
Is Student 1 male or female?
m
What is Student's 1's average?
85
Is Student 2 male or female?
m
What is Student's 2's average?
91
Is Student 3 male or female?
f
What is Student's 3's average?
95
Is Student 4 male or female?
f
What is Student's 4's average?
80
Is Student 5 male or female?
m
What is Student's 5's average?
88
The 3 male Students average is 88.0
The 2 male Students average is 87.5
*/

More Related Content

Similar to Average.javaimport java.util.Scanner;public class Average{ .pdf

Write the code above and the ones below in netbeans IDE 8.13. (Eli.pdf
Write the code above and the ones below in netbeans IDE 8.13. (Eli.pdfWrite the code above and the ones below in netbeans IDE 8.13. (Eli.pdf
Write the code above and the ones below in netbeans IDE 8.13. (Eli.pdf
arihantmum
 
The solution is as belowEmployeeDemo.javaimport java.util.Scann.pdf
The solution is as belowEmployeeDemo.javaimport java.util.Scann.pdfThe solution is as belowEmployeeDemo.javaimport java.util.Scann.pdf
The solution is as belowEmployeeDemo.javaimport java.util.Scann.pdf
aparnatiwari291
 
import java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdfimport java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdf
optokunal1
 
Create a menu-driven program that will accept a collection of non-ne.pdf
Create a menu-driven program that will accept a collection of non-ne.pdfCreate a menu-driven program that will accept a collection of non-ne.pdf
Create a menu-driven program that will accept a collection of non-ne.pdf
rajeshjangid1865
 
Im getting List Full when I try to add 2nd student.Driver..pdf
Im getting List Full when I try to add 2nd student.Driver..pdfIm getting List Full when I try to add 2nd student.Driver..pdf
Im getting List Full when I try to add 2nd student.Driver..pdf
forwardcom41
 
I have an interface Peron that has firstnamestring, lastnamestring.pdf
I have an interface Peron that has firstnamestring, lastnamestring.pdfI have an interface Peron that has firstnamestring, lastnamestring.pdf
I have an interface Peron that has firstnamestring, lastnamestring.pdf
lejeunehayneswowel96
 
L11a Create the Student class derived from the Person class- A student.docx
L11a Create the Student class derived from the Person class- A student.docxL11a Create the Student class derived from the Person class- A student.docx
L11a Create the Student class derived from the Person class- A student.docx
Jacob6ALMcDonaldu
 
Average.javaimport java.util.Scanner;   import java.util.Arra.pdf
 Average.javaimport java.util.Scanner;   import java.util.Arra.pdf Average.javaimport java.util.Scanner;   import java.util.Arra.pdf
Average.javaimport java.util.Scanner;   import java.util.Arra.pdf
anuragperipheral
 
Im having an issue with the simulateOPT() methodthis is the p.pdf
Im having an issue with the simulateOPT() methodthis is the p.pdfIm having an issue with the simulateOPT() methodthis is the p.pdf
Im having an issue with the simulateOPT() methodthis is the p.pdf
stopgolook
 
import java.util.Scanner;public class Digits { public static v.pdf
import java.util.Scanner;public class Digits { public static v.pdfimport java.util.Scanner;public class Digits { public static v.pdf
import java.util.Scanner;public class Digits { public static v.pdf
apexelectronices01
 
Javaa. The mean of a list of numbers is its arithmetic average. T.pdf
Javaa. The mean of a list of numbers is its arithmetic average. T.pdfJavaa. The mean of a list of numbers is its arithmetic average. T.pdf
Javaa. The mean of a list of numbers is its arithmetic average. T.pdf
RAJATCHUGH12
 

Similar to Average.javaimport java.util.Scanner;public class Average{ .pdf (18)

Write the code above and the ones below in netbeans IDE 8.13. (Eli.pdf
Write the code above and the ones below in netbeans IDE 8.13. (Eli.pdfWrite the code above and the ones below in netbeans IDE 8.13. (Eli.pdf
Write the code above and the ones below in netbeans IDE 8.13. (Eli.pdf
 
The solution is as belowEmployeeDemo.javaimport java.util.Scann.pdf
The solution is as belowEmployeeDemo.javaimport java.util.Scann.pdfThe solution is as belowEmployeeDemo.javaimport java.util.Scann.pdf
The solution is as belowEmployeeDemo.javaimport java.util.Scann.pdf
 
import java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdfimport java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdf
 
Create a menu-driven program that will accept a collection of non-ne.pdf
Create a menu-driven program that will accept a collection of non-ne.pdfCreate a menu-driven program that will accept a collection of non-ne.pdf
Create a menu-driven program that will accept a collection of non-ne.pdf
 
Im getting List Full when I try to add 2nd student.Driver..pdf
Im getting List Full when I try to add 2nd student.Driver..pdfIm getting List Full when I try to add 2nd student.Driver..pdf
Im getting List Full when I try to add 2nd student.Driver..pdf
 
I have an interface Peron that has firstnamestring, lastnamestring.pdf
I have an interface Peron that has firstnamestring, lastnamestring.pdfI have an interface Peron that has firstnamestring, lastnamestring.pdf
I have an interface Peron that has firstnamestring, lastnamestring.pdf
 
L11a Create the Student class derived from the Person class- A student.docx
L11a Create the Student class derived from the Person class- A student.docxL11a Create the Student class derived from the Person class- A student.docx
L11a Create the Student class derived from the Person class- A student.docx
 
Comp102 lec 5.1
Comp102   lec 5.1Comp102   lec 5.1
Comp102 lec 5.1
 
Ch5(loops)
Ch5(loops)Ch5(loops)
Ch5(loops)
 
Average.javaimport java.util.Scanner;   import java.util.Arra.pdf
 Average.javaimport java.util.Scanner;   import java.util.Arra.pdf Average.javaimport java.util.Scanner;   import java.util.Arra.pdf
Average.javaimport java.util.Scanner;   import java.util.Arra.pdf
 
Im having an issue with the simulateOPT() methodthis is the p.pdf
Im having an issue with the simulateOPT() methodthis is the p.pdfIm having an issue with the simulateOPT() methodthis is the p.pdf
Im having an issue with the simulateOPT() methodthis is the p.pdf
 
Code javascript
Code javascriptCode javascript
Code javascript
 
Lab01.pptx
Lab01.pptxLab01.pptx
Lab01.pptx
 
import java.util.Scanner;public class Digits { public static v.pdf
import java.util.Scanner;public class Digits { public static v.pdfimport java.util.Scanner;public class Digits { public static v.pdf
import java.util.Scanner;public class Digits { public static v.pdf
 
JAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdfJAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdf
 
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAMPROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
 
C Programming Language Part 9
C Programming Language Part 9C Programming Language Part 9
C Programming Language Part 9
 
Javaa. The mean of a list of numbers is its arithmetic average. T.pdf
Javaa. The mean of a list of numbers is its arithmetic average. T.pdfJavaa. The mean of a list of numbers is its arithmetic average. T.pdf
Javaa. The mean of a list of numbers is its arithmetic average. T.pdf
 

More from venkt12345

To insert value X into a B-tree, there are 3 stepsIf there are M .pdf
To insert value X into a B-tree, there are 3 stepsIf there are M .pdfTo insert value X into a B-tree, there are 3 stepsIf there are M .pdf
To insert value X into a B-tree, there are 3 stepsIf there are M .pdf
venkt12345
 
The physical protection of knowledge, assets and personnel is key to.pdf
The physical protection of knowledge, assets and personnel is key to.pdfThe physical protection of knowledge, assets and personnel is key to.pdf
The physical protection of knowledge, assets and personnel is key to.pdf
venkt12345
 
Purpose of cashflow statements are to analyse the different cashflow.pdf
Purpose of cashflow statements are to analyse the different cashflow.pdfPurpose of cashflow statements are to analyse the different cashflow.pdf
Purpose of cashflow statements are to analyse the different cashflow.pdf
venkt12345
 
Modern Times is one of the greatest movies in the history of film. T.pdf
Modern Times is one of the greatest movies in the history of film. T.pdfModern Times is one of the greatest movies in the history of film. T.pdf
Modern Times is one of the greatest movies in the history of film. T.pdf
venkt12345
 
Main components of a computerMultimedia devicesOther peripheral .pdf
Main components of a computerMultimedia devicesOther peripheral .pdfMain components of a computerMultimedia devicesOther peripheral .pdf
Main components of a computerMultimedia devicesOther peripheral .pdf
venkt12345
 
INTRODUCTION TO COAL MINING INDUSTRYEconomic growth world over is .pdf
INTRODUCTION TO COAL MINING INDUSTRYEconomic growth world over is .pdfINTRODUCTION TO COAL MINING INDUSTRYEconomic growth world over is .pdf
INTRODUCTION TO COAL MINING INDUSTRYEconomic growth world over is .pdf
venkt12345
 
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdfimport java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
venkt12345
 
Identify the computer fraud and abuse technique byThis computer f.pdf
Identify the computer fraud and abuse technique byThis computer f.pdfIdentify the computer fraud and abuse technique byThis computer f.pdf
Identify the computer fraud and abuse technique byThis computer f.pdf
venkt12345
 
The short answer is no. The reason why you cant p.pdf
                     The short answer is no. The reason why you cant p.pdf                     The short answer is no. The reason why you cant p.pdf
The short answer is no. The reason why you cant p.pdf
venkt12345
 
First, lets start with naming binary ionic comp.pdf
                     First, lets start with naming binary ionic comp.pdf                     First, lets start with naming binary ionic comp.pdf
First, lets start with naming binary ionic comp.pdf
venkt12345
 

More from venkt12345 (20)

To insert value X into a B-tree, there are 3 stepsIf there are M .pdf
To insert value X into a B-tree, there are 3 stepsIf there are M .pdfTo insert value X into a B-tree, there are 3 stepsIf there are M .pdf
To insert value X into a B-tree, there are 3 stepsIf there are M .pdf
 
The physical protection of knowledge, assets and personnel is key to.pdf
The physical protection of knowledge, assets and personnel is key to.pdfThe physical protection of knowledge, assets and personnel is key to.pdf
The physical protection of knowledge, assets and personnel is key to.pdf
 
The function has 11 zeros , since the degree of function is 11So.pdf
The function has 11 zeros , since the degree of function is 11So.pdfThe function has 11 zeros , since the degree of function is 11So.pdf
The function has 11 zeros , since the degree of function is 11So.pdf
 
Purpose of cashflow statements are to analyse the different cashflow.pdf
Purpose of cashflow statements are to analyse the different cashflow.pdfPurpose of cashflow statements are to analyse the different cashflow.pdf
Purpose of cashflow statements are to analyse the different cashflow.pdf
 
O,S as they belong to same groupSolutionO,S as they belong to .pdf
O,S as they belong to same groupSolutionO,S as they belong to .pdfO,S as they belong to same groupSolutionO,S as they belong to .pdf
O,S as they belong to same groupSolutionO,S as they belong to .pdf
 
Modern Times is one of the greatest movies in the history of film. T.pdf
Modern Times is one of the greatest movies in the history of film. T.pdfModern Times is one of the greatest movies in the history of film. T.pdf
Modern Times is one of the greatest movies in the history of film. T.pdf
 
Main components of a computerMultimedia devicesOther peripheral .pdf
Main components of a computerMultimedia devicesOther peripheral .pdfMain components of a computerMultimedia devicesOther peripheral .pdf
Main components of a computerMultimedia devicesOther peripheral .pdf
 
INTRODUCTION TO COAL MINING INDUSTRYEconomic growth world over is .pdf
INTRODUCTION TO COAL MINING INDUSTRYEconomic growth world over is .pdfINTRODUCTION TO COAL MINING INDUSTRYEconomic growth world over is .pdf
INTRODUCTION TO COAL MINING INDUSTRYEconomic growth world over is .pdf
 
Include time header file#includetime.hWrite main method like t.pdf
Include time header file#includetime.hWrite main method like t.pdfInclude time header file#includetime.hWrite main method like t.pdf
Include time header file#includetime.hWrite main method like t.pdf
 
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdfimport java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
 
Identify the computer fraud and abuse technique byThis computer f.pdf
Identify the computer fraud and abuse technique byThis computer f.pdfIdentify the computer fraud and abuse technique byThis computer f.pdf
Identify the computer fraud and abuse technique byThis computer f.pdf
 
Given A fund that starts with a zero balance with time zero. Fund .pdf
Given  A fund that starts with a zero balance with time zero. Fund .pdfGiven  A fund that starts with a zero balance with time zero. Fund .pdf
Given A fund that starts with a zero balance with time zero. Fund .pdf
 
Your equation is correct. Double replacement reac.pdf
                     Your equation is correct. Double replacement reac.pdf                     Your equation is correct. Double replacement reac.pdf
Your equation is correct. Double replacement reac.pdf
 
well while idenitfying aldehydeketone u must kee.pdf
                     well while idenitfying aldehydeketone u must kee.pdf                     well while idenitfying aldehydeketone u must kee.pdf
well while idenitfying aldehydeketone u must kee.pdf
 
The short answer is no. The reason why you cant p.pdf
                     The short answer is no. The reason why you cant p.pdf                     The short answer is no. The reason why you cant p.pdf
The short answer is no. The reason why you cant p.pdf
 
Since it gets in equilibrium when dissolved at sa.pdf
                     Since it gets in equilibrium when dissolved at sa.pdf                     Since it gets in equilibrium when dissolved at sa.pdf
Since it gets in equilibrium when dissolved at sa.pdf
 
phosphite ion .pdf
                     phosphite ion                                    .pdf                     phosphite ion                                    .pdf
phosphite ion .pdf
 
First, lets start with naming binary ionic comp.pdf
                     First, lets start with naming binary ionic comp.pdf                     First, lets start with naming binary ionic comp.pdf
First, lets start with naming binary ionic comp.pdf
 
dFdy=dFdu dudy +dFdv dvdy =e^(u+v) 0 +e.pdf
                     dFdy=dFdu  dudy +dFdv dvdy =e^(u+v)  0 +e.pdf                     dFdy=dFdu  dudy +dFdv dvdy =e^(u+v)  0 +e.pdf
dFdy=dFdu dudy +dFdv dvdy =e^(u+v) 0 +e.pdf
 
At STP22.4 L Is Equivalent to 1 moleThereforeNo. of Moles = 31.pdf
At STP22.4 L Is Equivalent to 1 moleThereforeNo. of Moles = 31.pdfAt STP22.4 L Is Equivalent to 1 moleThereforeNo. of Moles = 31.pdf
At STP22.4 L Is Equivalent to 1 moleThereforeNo. of Moles = 31.pdf
 

Recently uploaded

SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 

Recently uploaded (20)

Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge App
 
demyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxdemyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptx
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptx
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 

Average.javaimport java.util.Scanner;public class Average{ .pdf

  • 1. // Average.java import java.util.Scanner; public class Average{ public static void main(String[] args){ Scanner input = new Scanner(System.in); double averageMale = 0, averageFemale = 0; int countMale = 0, countFemale = 0; char mf; double average; System.out.println("How many Students? "); int n = input.nextInt(); for (int i = 1; i <= n; i++ ) { System.out.println("Is Student " + i + " male or female? "); mf = input.next().charAt(0); if(mf == 'm'){ countMale++; System.out.println("What is Student's " + i + "'s average? "); average = input.nextDouble(); averageMale = averageMale + average; } else{ countFemale++; System.out.println("What is Student's " + i + "'s average? "); average = input.nextDouble(); averageFemale = averageFemale + average; } } System.out.println("The " + countMale + " male Students average is " + averageMale/countMale); System.out.println("The " + countFemale + " male Students average is " + averageFemale/countFemale); }
  • 2. } /* output: How many Students? 5 Is Student 1 male or female? m What is Student's 1's average? 85 Is Student 2 male or female? m What is Student's 2's average? 91 Is Student 3 male or female? f What is Student's 3's average? 95 Is Student 4 male or female? f What is Student's 4's average? 80 Is Student 5 male or female? m What is Student's 5's average? 88 The 3 male Students average is 88.0 The 2 male Students average is 87.5 */ Solution // Average.java import java.util.Scanner; public class Average{
  • 3. public static void main(String[] args){ Scanner input = new Scanner(System.in); double averageMale = 0, averageFemale = 0; int countMale = 0, countFemale = 0; char mf; double average; System.out.println("How many Students? "); int n = input.nextInt(); for (int i = 1; i <= n; i++ ) { System.out.println("Is Student " + i + " male or female? "); mf = input.next().charAt(0); if(mf == 'm'){ countMale++; System.out.println("What is Student's " + i + "'s average? "); average = input.nextDouble(); averageMale = averageMale + average; } else{ countFemale++; System.out.println("What is Student's " + i + "'s average? "); average = input.nextDouble(); averageFemale = averageFemale + average; } } System.out.println("The " + countMale + " male Students average is " + averageMale/countMale); System.out.println("The " + countFemale + " male Students average is " + averageFemale/countFemale); } } /*
  • 4. output: How many Students? 5 Is Student 1 male or female? m What is Student's 1's average? 85 Is Student 2 male or female? m What is Student's 2's average? 91 Is Student 3 male or female? f What is Student's 3's average? 95 Is Student 4 male or female? f What is Student's 4's average? 80 Is Student 5 male or female? m What is Student's 5's average? 88 The 3 male Students average is 88.0 The 2 male Students average is 87.5 */