No older revisions available
No older revisions available
~cpp TheTrip.java
¶
~cpp
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
/*
* Created on 2005. 1. 4
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class TheTrip {
int studentNum;
double [] money;
double average;
double movedMoney;
public int inputStudentNum() {
studentNum = (int) inputNum();
return studentNum;
}
private double inputNum() {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
double num = 0.00;
try {
String line = in.readLine();
num = Double.parseDouble(line);
} catch (IOException e) {
e.printStackTrace();
}
return num;
}
public double[] inputMoney() {
money = new double[studentNum];
for(int i = 0; i < studentNum; i++) {
money[i] = inputNum();
while (money[i] > 10000.00) {
System.out.println("$10000.00을 넘을수없으니 다시 입력해");
money[i] = inputNum();
}
}
return money;
}
public double setMovedMoney() {
for(int i = 0; i < money.length; i++) {
double difference = money[i] - average;
if (difference > 0) movedMoney += difference;
}
movedMoney = convertToTwoDigits(movedMoney);
return movedMoney;
}
public double setAverage() {
double sum = 0.00;
for(int i = 0; i < money.length; i++)
sum += money[i];
sum = convertToTwoDigits(sum);
average = convertToTwoDigits(sum / money.length);
return average;
}
public double convertToTwoDigits(double aNum) {
aNum *= 100;
aNum = Math.ceil(aNum);
aNum /= 100;
return aNum;
}
private void printResult(ArrayList list) {
for(int i = 0; i < list.size(); i++) {
Double num = (Double) list.get(i);
System.out.println("$" + num.doubleValue());
}
}
static public void main(String [] args) {
ArrayList list = new ArrayList();
while(true) {
TheTrip trip = new TheTrip();
int student = trip.inputStudentNum();
while (student >= 1000) {
System.out.println("학생수는 1000명을 넘지 않으니 다시 입력해");
student = trip.inputStudentNum();
}
if (trip.studentNum == 0) {
trip.printResult(list);
break;
}
trip.inputMoney();
trip.setAverage();
trip.setMovedMoney();
Double num = new Double(trip.movedMoney);
list.add(num);
}
}
}
쓰레드 ¶
- double형으로 숫자를 입력받아서 소수점 처리를 하는데 고생했다 -- 재선