• 検索結果がありません。

48

49 処理内容

コードと売上高を1件ずつ入力し、配列totalSalesに売上高を集計する。コード に999が入力されると合計と平均を表示して終了する。なお、コードは0~9で配

列totalSalesの添字に対応している。

実行結果

コードと売上高を入力:

1 320 4 550 3 800 7 230 999

合計:1900 平均:475 プログラム

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

import java.util.Scanner;

public class TotalSample {

public static void main(String[] args) { int[] totalSales = new int[10];

int sum = 0, count = 0;

int code, sales;

int avg;

Scanner sc = new Scanner(System.in);

System.out.println("コードと売上高を入力:");

while(( (1) = sc.nextInt()) != 999){

(2) = sc.nextInt();

totalSales[ (3) ] += sales;

sum += sales;

count++;

}

avg = (4) ;

System.out.println("合計:" + sum + " 平均:" + avg);

sc.close();

} }

(1) (2) (3) (4)

問題 7 - 2 教科書 P103(2 級範囲)

50

第7章 配列を用いた集計

処理内容

コードと売上高を入力し、売上高を配列salesに集計する。配列salesはコードと 添字で対応している。入力が終わると売上高を合計に占める割合とともに表示す る。

実行結果

0~3 のコードを入力(99 で終了):0 売上高を入力:300

0~3 のコードを入力(99 で終了):2 売上高を入力:400

0~3 のコードを入力(99 で終了):3 売上高を入力:500

0~3 のコードを入力(99 で終了):99

0 300 25%

1 0 0%

2 400 33%

3 500 41%

合計 1200 プログラム

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

import java.util.Scanner;

public class SalesTest {

public static void main(String[] args) { int code, inputSales, sum = 0;

int[] sales = {0, 0, 0, 0};

Scanner sc = new Scanner(System.in);

System.out.print("0~3 のコードを入力(99 で終了):");

while((code = sc.nextInt()) != 99){

System.out.print("売上高を入力:");

inputSales = sc.nextInt();

sales[ (1) ] += inputSales;

(2) ;

System.out.print("0~3 のコードを入力(99 で終了):");

}

int percent;

for(int i = 0; i < sales.length; i++){

percent = sales[i] * 100 / sum;

System.out.printf("%d\t%d\t%d%%\n",i,sales[i], (3) );

}

System.out.println("合計\t" + sum);

sc.close();

} }

(1) (2) (3)

問題 7 - 3 教科書 P103(2 級範囲)

51 処理内容

5 桁の売上コードを入力し、支店ごとに売上高を集計する。売上コードの左端 1 桁が支店コードを表し、残りの4桁が売上高を表す。なお、支店コードは1~3で ある。支店別の売上高は配列sitenSalesに集計する。

実行結果

5 桁の売上コードを入力して下さい:15320 5 桁の売上コードを入力して下さい:23800 5 桁の売上コードを入力して下さい:32000 5 桁の売上コードを入力して下さい:21000 5 桁の売上コードを入力して下さい:99999 1 5320

2 4800 3 2000 プログラム

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

import java.util.Scanner;

public class CodeSample {

public static void main(String[] args) { int code, sitenCode, sales;

int[] sitenSales = {0, 0, 0};

Scanner sc = new Scanner(System.in);

System.out.print("5 桁の売上コードを入力して下さい:");

code = sc.nextInt();

while(code != 99999){

sitenCode = code / 10000;

sales = code - (1) ; (2) += sales;

System.out.print("5 桁の売上コードを入力して下さい:");

(3) ; }

for(int i = 0; i < sitenSales.length; i++){

System.out.println(i + 1 + "\t" + sitenSales[i]);

}

sc.close();

} }

(1) (2) (3)

問題 7 - 4 教科書 P103(2 級範囲)

52

第7章 配列を用いた集計

処理内容

金額を入力すると、その金額を支払うのに必要な金種と枚数を表示する。

実行結果

金額を入力:98765 10,000 円 9 枚 5,000 円 1 枚 1,000 円 3 枚 500 円 1 枚 100 円 2 枚 50 円 1 枚 10 円 1 枚 5 円 1 枚 1 円 0 枚 プログラム

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

import java.util.Scanner;

public class KinsyuKeisan {

public static void main(String[] args) {

int[] kinsyu = {10000, 5000, 1000, 500, 100, 50, 10, 5, 1};

int[] number = {0, 0, 0, 0, 0, 0, 0, 0, 0};

int kingaku;

Scanner sc = new Scanner(System.in);

System.out.print("金額を入力:");

kingaku = sc.nextInt();

for(int i = 0; i < kinsyu.length; i++){

number[i] = kingaku / (1) ;

kingaku = kingaku - (2) * kinsyu[i];

}

for(int i = 0; i < kinsyu.length; i++){

System.out.printf("%,6d 円\t", kinsyu[i]);

System.out.println(number[i] + "枚");

}

sc.close();

} }

(1) (2)

問題 7 - 5 教科書 P103(2 級範囲)

53 処理内容

参加者の年齢を入力し、下記の配列に集計する。なお、参加者は20歳以上である。

年齢に999を入力すると集計結果を出力する。

(0) (1) (2) (3) (4)

syuukei

20代 30代 40代 50代 60 代以上 実行結果

参加者の年齢を入力:25 参加者の年齢を入力:52 参加者の年齢を入力:34 参加者の年齢を入力:70 参加者の年齢を入力:63 参加者の年齢を入力:44 参加者の年齢を入力:999

20 代 30 代 40 代 50 代 60 代以上 1 人 1 人 1 人 1 人 2 人 プログラム

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

import java.util.Scanner;

public class NenreiSyuukei {

public static void main(String[] args) { int sankasya, nendai;

int[] syuukei = {0, 0, 0, 0, 0};

Scanner sc = new Scanner(System.in);

System.out.print("参加者の年齢を入力:");

sankasya = sc.nextInt();

while(sankasya != 999){

nendai = sankasya / 10;

if(nendai >= 6) (1) ; else

(2) ; syuukei[nendai]++;

System.out.print("参加者の年齢を入力:");

sankasya = sc.nextInt();

}

System.out.println("20 代\t30 代\t40 代\t50 代\t60 代以上");

for(int i = 0; i < syuukei.length; i++){

System.out.print( (3) + "人\t");

}

sc.close();

} }

(1) (2) (3)

問題 7 - 6 2 級範囲

54

関連したドキュメント