2009年3月25日 星期三

Homework: 3/23/2009

Write a program to calculate average income by gender based on the following data, where F stands for female and M for male.

F 62,000
M 25,000
F 38,000
F 43,000
M 65,000
M 120,000
F 80,000
M 30,100

You should be able to allow users to type in a whole line such as F 80,000 followed by next line M 30,100.

------------------------------------------------<程式碼開始>--------------------------------------

import java.util.*;
public class Homework03232009 {
    public static void main (String args[]) {
Scanner Keyboard = new Scanner(System.in);
        int mn=0, fn=0, le=1;
double mTotal=0, fTotal=0;
String sex;
do{
System.out.print("enter:");
sex = Keyboard.next();
if(sex.equals("M")){
mTotal = Keyboard.nextDouble();
mn++;
}
else if(sex.equals("F")){
fTotal = Keyboard.nextDouble();
fn++;
}
else if(sex.equals("exit")){
System.out.println("End");
le = 0;
}
else
System.out.println("error.\n");
}while(le==1);
System.out.println("Total people: " + (mTotal + fTotal));
System.out.println("Male's Avg = " + mTotal/mn);
System.out.println("Female's Avg = " + fTotal/fn);
    }
}

------------------------------------------------<程式碼結束>--------------------------------------
附註:本次也是參考以繳交之同學作業改寫,新增了偵錯輸入這部份。這次學到更多的字串應用方式。

沒有留言:

張貼留言