marunomaruno-memo

marunomaruno-memo

Java問題 サンプル解答 2次元配列

2010年07月12日 | Weblog
--------------------------------------------------
import java.util.Scanner;

/**
 * Quiz0904で、じゃんけんの勝敗表(2次
 * 元配列)をつくり、勝負するロジックを簡単
 * にする。
 * また、指定した手も、数字ではなく、日本語
 * 表記(グー、チョキ、パー)で表示すること。
 * --------- ------- -------- --------
 * 勝敗表    0 グー  1 チョキ  2 パー
 * --------- ------- -------- --------
 * 0 グー    引分け    勝ち     負け
 * 1 チョキ   負け    引分け    勝ち
 * 2 パー     勝ち     負け    引分け
 * --------- ------- -------- --------
 */
public class Quiz1621 {
    public static void main(String[] args) {
        String[] HAND = {"グー", "チョキ", "パー"};
        int[][] JUDGE = {
                { 0,  1, -1},     // ぐー
                {-1,  0,  1},     // ちょき
                { 1, -1,  0},     // ぱー
        };
        
        // 標準入力を準備する
        Scanner sin = new Scanner(System.in);

        // じゃんけんの手を入力する
        System.out.print("じゃんけんの手(0:グー、 1:チョキ、 2:パー )を入力してください => ");
        int userTe = sin.nextInt(); // 標準入力から手を入力する
        int computerTe = (int) (Math.random() * 3); // コンピューターの手

        // 勝負する
        int judge = JUDGE[userTe][computerTe];        // 勝敗結果 -1: 負け、0: 引分け、1: 勝ち

        // 結果を表示する
        System.out.print("あなた: " + HAND[userTe] + ", コンピューター: " + HAND[computerTe]);
        if (judge == 0) {
            System.out.println(" 引き分けです。");
        } else if (judge > 0) {
            System.out.println(" あなたの勝ちです。");
        } else {
            System.out.println(" あなたの負けです。");
        }
    }
}

--------------------------------------------------
import java.util.Scanner;

/**
 * 1000円でお釣りをもらう。お釣りの硬貨は最低枚数とする。
 * 6×2の配列の、2列目に、それぞれのお釣りの枚数を入れるアルゴリズムを作りなさい
 * 1列目には硬貨の種類があらかじめ入っているものとする。
 */
public class Quiz1622 {
    public static void main(String[] args) {
        String buffer;    // データを入力するバッファー

        int coins[][] = {    // 硬貨を数える配列
                {1, 0},
                {5, 0},
                {10, 0},
                {50, 0},
                {100, 0},
                {500, 0},
        };

        int mony;              // 金額
        int change;            // お釣り

        // 標準入力を設定する
        Scanner in = new Scanner(System.in);

        // データを入力する。
        System.out.printf("金額を入力してください。");
        buffer = in.nextLine();
        mony = Integer.parseInt(buffer);

        // お釣り
        change = 1000 - mony;
        System.out.println("金額は " + mony + " 円、お釣りは " + change + " 円です。");

        // 枚数を数える
        for (int i = coins.length - 1; i >= 0; i--) {
            coins[i][1] = change / coins[i][0];
            change %= coins[i][0];
        }

        // 枚数を表示する
        for (int i = 0; i < coins.length; i++) {
            System.out.println(coins[i][0] + " 円硬貨は " + coins[i][1] + " 枚です。");
        }
    }
}

--------------------------------------------------
import java.util.Scanner;

/**
 * 23.重さに対する料金を出す。
 * 6×2の配列の、1列目に重さ、2列目に料金が
 * 入っている。
 * なお、重さは、下の表では以下をあらわす。
 * たとえば、200gに対しては、190円になる。
 * 2000gより重い場合は、一律に2000円とする
 * 
 *     ------- ----
 *     重さ    料金
 *     ------- ----
 * [0]  100     100
 * [1]  200     190
 * [2]  400     370
 * [3]  600     550
 * [4] 1000     900
 * [5] 2000    1700
 *     ------- ----
 */
public class Quiz1623 {
    public static void main(String[] args) {
        String buffer;    // データを入力するバッファー

        int WEIGHT_RATE[][] = {    // 重量-料金表
                { 100,  100},
                { 200,  190},
                { 400,  370},
                { 600,  550},
                {1000,  900},
                {2000, 1700},
        };

        int weight;              // 重量

        // 標準入力を設定する
        Scanner in = new Scanner(System.in);

        // データを入力する。
        System.out.printf("重量を入力してください。");
        buffer = in.nextLine();
        weight = Integer.parseInt(buffer);

        // 料金を引く
        int rate = 2000;                // 料金
        for (int i = 0; i < WEIGHT_RATE.length; i++) {
            if (weight <= WEIGHT_RATE[i][0]) {
                rate = WEIGHT_RATE[i][1];
                break;
            }
        }

        // 結果を表示する
        System.out.println(weight + " g の料金は " + rate + " 円です。");
    }
}

--------------------------------------------------


最新の画像もっと見る

コメントを投稿