1.
2重for文を使って以下の見出し付き九九表を作成せよ。
01 | --------------------------------------------------- |
02 | | X | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
03 | --------------------------------------------------- |
04 | | 1 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
05 | --------------------------------------------------- |
06 | | 2 | 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | |
07 | --------------------------------------------------- |
08 | | 3 | 3 | 6 | 9 | 12 | 15 | 18 | 21 | 24 | 27 | |
09 | --------------------------------------------------- |
10 | | 4 | 4 | 8 | 12 | 16 | 20 | 24 | 28 | 32 | 36 | |
11 | --------------------------------------------------- |
12 | | 5 | 5 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | |
13 | --------------------------------------------------- |
14 | | 6 | 6 | 12 | 18 | 24 | 30 | 36 | 42 | 48 | 54 | |
15 | --------------------------------------------------- |
16 | | 7 | 7 | 14 | 21 | 28 | 35 | 42 | 49 | 56 | 63 | |
17 | --------------------------------------------------- |
18 | | 8 | 8 | 16 | 24 | 32 | 40 | 48 | 56 | 64 | 72 | |
19 | --------------------------------------------------- |
20 | | 9 | 9 | 18 | 27 | 36 | 45 | 54 | 63 | 72 | 81 | |
21 | --------------------------------------------------- |
解)
01 | public class Ninenine { |
03 | public static void main(String[] args) { |
07 | int width=String.valueOf(MAX*MAX).length()+ 1 ; |
09 | String format= "|%" +(width)+ "s " ; |
11 | int lineWidth=(width+ 2 )*(MAX+ 1 )+ 1 ; |
13 | for ( int i= 0 ;i<=MAX;i++){ |
17 | for ( int j= 0 ;j<=MAX;j++){ |
25 | str=String.valueOf(j); |
30 | str=String.valueOf(i); |
33 | str=String.valueOf(i*j); |
37 | System.out.printf(format,str); |
40 | System.out.println( "|" ); |
46 | static void printLine( int width){ |
47 | for ( int i= 0 ;i<width;i++){ |
48 | System.out.print( "-" ); |
2.
上で作成したものを変更して以下のようなアプリに仕上げよ
[実行例]
01 | 1からいくつまでの掛け算表を作成しますか(0で終了)>5 |
02 | ------------------------------- |
03 | | X | 1 | 2 | 3 | 4 | 5 | |
04 | ------------------------------- |
05 | | 1 | 1 | 2 | 3 | 4 | 5 | |
06 | ------------------------------- |
07 | | 2 | 2 | 4 | 6 | 8 | 10 | |
08 | ------------------------------- |
09 | | 3 | 3 | 6 | 9 | 12 | 15 | |
10 | ------------------------------- |
11 | | 4 | 4 | 8 | 12 | 16 | 20 | |
12 | ------------------------------- |
13 | | 5 | 5 | 10 | 15 | 20 | 25 | |
14 | ------------------------------- |
15 | 1からいくつまでの掛け算表を作成しますか(0で終了)>10 |
16 | ------------------------------------------------------------------- |
17 | | X | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | |
18 | ------------------------------------------------------------------- |
19 | | 1 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | |
20 | ------------------------------------------------------------------- |
21 | | 2 | 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | 20 | |
22 | ------------------------------------------------------------------- |
23 | | 3 | 3 | 6 | 9 | 12 | 15 | 18 | 21 | 24 | 27 | 30 | |
24 | ------------------------------------------------------------------- |
25 | | 4 | 4 | 8 | 12 | 16 | 20 | 24 | 28 | 32 | 36 | 40 | |
26 | ------------------------------------------------------------------- |
27 | | 5 | 5 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | |
28 | ------------------------------------------------------------------- |
29 | | 6 | 6 | 12 | 18 | 24 | 30 | 36 | 42 | 48 | 54 | 60 | |
30 | ------------------------------------------------------------------- |
31 | | 7 | 7 | 14 | 21 | 28 | 35 | 42 | 49 | 56 | 63 | 70 | |
32 | ------------------------------------------------------------------- |
33 | | 8 | 8 | 16 | 24 | 32 | 40 | 48 | 56 | 64 | 72 | 80 | |
34 | ------------------------------------------------------------------- |
35 | | 9 | 9 | 18 | 27 | 36 | 45 | 54 | 63 | 72 | 81 | 90 | |
36 | ------------------------------------------------------------------- |
37 | | 10 | 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 | 100 | |
38 | ------------------------------------------------------------------- |
39 | 1からいくつまでの掛け算表を作成しますか(0で終了)>0 |
解)
01 | import java.util.Scanner; |
03 | public class Ninenine { |
05 | public static void main(String[] args) { |
07 | Scanner scan= new Scanner(System.in); |
10 | System.out.print( "1からいくつまでの掛け算表を作成しますか(0で終了)>" ); |
11 | int max=scan.nextInt(); |
14 | System.out.println( "アプリを終了します" ); |
24 | static void printLine( int width) { |
25 | for ( int i = 0 ; i < width; i++) { |
26 | System.out.print( "-" ); |
31 | static void createTable( int max) { |
34 | int width = String.valueOf(max * max).length() + 1 ; |
36 | String format = "|%" + (width) + "s " ; |
38 | int lineWidth = (width + 2 ) * (max + 1 ) + 1 ; |
40 | for ( int i = 0 ; i <= max; i++) { |
44 | for ( int j = 0 ; j <= max; j++) { |
52 | str = String.valueOf(j); |
57 | str = String.valueOf(i); |
60 | str = String.valueOf(i * j); |
64 | System.out.printf(format, str); |
67 | System.out.println( "|" ); |
コメント