// 1つ目のクラスのオブジェクトを作成し、メソッドを呼び出すクラス import java.io.*; public class AllowanceCalling { public static void main(String[] args) { try { String incomeString, detailString; int income, outgo, detail; double rate; BufferedReader br = new BufferedReader (new InputStreamReader(System.in)); System.out.println("収入の金額を入力してください。"); System.out.print("収入の金額: "); // incomeString: 収入の金額を標準入力で入力 incomeString = br.readLine(); // income: 入力された収入の金額を int 型に変換 income = Integer.parseInt(incomeString); System.out.println("支出の金額を1つずつ入力してください。すべて入力し終わったら「End」と入力してください。"); System.out.print("支出の金額: "); // detailString: 支出の金額を 1 つずつ標準入力で入力 detailString = br.readLine(); // outgo: 支出の金額の合計 outgo = 0; // 標準入力から「End」と入力されるまで、支出の金額の入力を繰り返し while(!detailString.equals("End")) { // detail: 入力された支出の金額を int 型に変換 detail = Integer.parseInt(detailString); // 支出の金額の合計を計算 outgo = outgo + detail; System.out.print("支出の金額: "); detailString = br.readLine(); } // おこづかい帳の収入と支出を管理するクラス (Allowance) のオブジェクトを作成 Allowance al = new Allowance(); // 収入と支出の金額を、作成したオブジェクトに設定 al.income = income; al.outgo = outgo; // 収入に対する支出の割合を、パーセンテージで計算 rate = al.calculateRate(); System.out.println("収入に対する支出の割合は" + rate + "%です。"); } catch(IOException e) { } } }