回答編集履歴

11

今更だけどデバッグ

2017/12/25 11:08

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -14,7 +14,7 @@
14
14
 
15
15
  static int retNDigitsNumWithoutOverlap(int digitNum) {
16
16
 
17
- if(digitNum < 0) throw new IllegalArgumentException();
17
+ if(digitNum <= 0) throw new IllegalArgumentException();
18
18
 
19
19
  if(10 < digitNum) throw new IllegalArgumentException();
20
20
 

10

追記

2017/12/25 11:08

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -72,6 +72,26 @@
72
72
 
73
73
 
74
74
 
75
+ 巨大な数が入力されないと保証できるのであれば、次のように書いても良いでしょう。
76
+
77
+ ```Java
78
+
79
+ static int computeNumOfDigit(int num) {
80
+
81
+ if(num < 0) throw new IllegalArgumentException();
82
+
83
+ for(int i = 1, p = 10; true; ++i, p *= 10) {
84
+
85
+ if(num < p) return i;
86
+
87
+ }
88
+
89
+ }
90
+
91
+ ```
92
+
93
+
94
+
75
95
  **『n桁の数値が入力されるまで、再入力を求める静的メソッド』のサンプル**
76
96
 
77
97
  ```Java

9

修正

2017/12/24 09:25

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -108,7 +108,7 @@
108
108
 
109
109
 
110
110
 
111
- 二つを組み合わせる、次のようなことが出来ます。
111
+ 実行イメージはのとおりです。
112
112
 
113
113
  ```
114
114
 

8

コード変更

2017/12/24 09:15

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -76,7 +76,7 @@
76
76
 
77
77
  ```Java
78
78
 
79
- static int scanNDigitInteger(String message, int digitNum) throws IOException {
79
+ static int scanInteger(BufferedReader br, String message) throws IOException {
80
80
 
81
81
  while(true) {
82
82
 
@@ -84,13 +84,21 @@
84
84
 
85
85
  System.out.print(message);
86
86
 
87
- int input = Integer.parseInt(br.readLine());
87
+ return Integer.parseInt(br.readLine());
88
-
89
-
90
-
91
- if(computeNumOfDigit(input) == digitNum) return input;
92
88
 
93
89
  } catch(NumberFormatException e) {}
90
+
91
+ }
92
+
93
+ }
94
+
95
+ static int scanNDigitInteger(BufferedReader br, String message, int digitNum) throws IOException {
96
+
97
+ while(true) {
98
+
99
+ int input = scanInteger(br, message);
100
+
101
+ if(computeNumOfDigit(input) == digitNum) return input;
94
102
 
95
103
  }
96
104
 

7

修正

2017/12/24 09:14

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -22,11 +22,11 @@
22
22
 
23
23
  List<Integer> digits = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
24
24
 
25
- while(digits.get(0) == 0) {
25
+ do {
26
26
 
27
27
  Collections.shuffle(digits);
28
28
 
29
- }
29
+ } while(digits.get(0) == 0);
30
30
 
31
31
 
32
32
 

6

修正

2017/12/24 09:02

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -72,7 +72,7 @@
72
72
 
73
73
 
74
74
 
75
- **『n桁の数値が入力されるまで、再入力を求めるメソッド』のサンプル**
75
+ **『n桁の数値が入力されるまで、再入力を求める静的メソッド』のサンプル**
76
76
 
77
77
  ```Java
78
78
 

5

追記

2017/12/24 09:01

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -69,3 +69,51 @@
69
69
  }
70
70
 
71
71
  ```
72
+
73
+
74
+
75
+ **『n桁の数値が入力されるまで、再入力を求めるメソッド』のサンプル**
76
+
77
+ ```Java
78
+
79
+ static int scanNDigitInteger(String message, int digitNum) throws IOException {
80
+
81
+ while(true) {
82
+
83
+ try {
84
+
85
+ System.out.print(message);
86
+
87
+ int input = Integer.parseInt(br.readLine());
88
+
89
+
90
+
91
+ if(computeNumOfDigit(input) == digitNum) return input;
92
+
93
+ } catch(NumberFormatException e) {}
94
+
95
+ }
96
+
97
+ }
98
+
99
+ ```
100
+
101
+
102
+
103
+ 以上の二つを組み合わせると、次のようなことが出来ます。
104
+
105
+ ```
106
+
107
+ input 4 digit num: hoge
108
+
109
+ input 4 digit num: asffdsa
110
+
111
+ input 4 digit num: 23
112
+
113
+ input 4 digit num: 34234
114
+
115
+ input 4 digit num: 3425
116
+
117
+ 3425
118
+
119
+ ```

4

修正

2017/12/24 09:01

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -60,11 +60,11 @@
60
60
 
61
61
  if(num < 0) throw new IllegalArgumentException();
62
62
 
63
- if(num == 0 || num == 1) return 1;
63
+ if(num == 0) return 1;
64
64
 
65
65
 
66
66
 
67
- return (int) Math.ceil(Math.log10(num));
67
+ return (int)Math.log10(num) + 1;
68
68
 
69
69
  }
70
70
 

3

修正

2017/12/24 08:50

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -60,7 +60,7 @@
60
60
 
61
61
  if(num < 0) throw new IllegalArgumentException();
62
62
 
63
- if(num == 0) return 1;
63
+ if(num == 0 || num == 1) return 1;
64
64
 
65
65
 
66
66
 

2

追記

2017/12/24 08:48

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -47,3 +47,25 @@
47
47
  }
48
48
 
49
49
  ```
50
+
51
+
52
+
53
+ ---
54
+
55
+ **『正の整数の桁数を計算する静的メソッド』のサンプル**
56
+
57
+ ```Java
58
+
59
+ static int computeNumOfDigit(int num) {
60
+
61
+ if(num < 0) throw new IllegalArgumentException();
62
+
63
+ if(num == 0) return 1;
64
+
65
+
66
+
67
+ return (int) Math.ceil(Math.log10(num));
68
+
69
+ }
70
+
71
+ ```

1

追記

2017/12/24 08:46

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -1,3 +1,49 @@
1
1
  単に『4桁であるか』が分かればいいのであれば、`1000 <= x < 10000`で充分なのでは。
2
2
 
3
3
  ただし、期待される入力は正の整数とします。
4
+
5
+
6
+
7
+ サンプル
8
+
9
+ ---
10
+
11
+ **『数字に重複のない、n桁の数値を返す静的メソッド』のサンプル**
12
+
13
+ ```Java
14
+
15
+ static int retNDigitsNumWithoutOverlap(int digitNum) {
16
+
17
+ if(digitNum < 0) throw new IllegalArgumentException();
18
+
19
+ if(10 < digitNum) throw new IllegalArgumentException();
20
+
21
+
22
+
23
+ List<Integer> digits = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
24
+
25
+ while(digits.get(0) == 0) {
26
+
27
+ Collections.shuffle(digits);
28
+
29
+ }
30
+
31
+
32
+
33
+ int ret = 0;
34
+
35
+ for(int digit: digits.subList(0, digitNum)) {
36
+
37
+ ret *= 10;
38
+
39
+ ret += digit;
40
+
41
+ }
42
+
43
+
44
+
45
+ return ret;
46
+
47
+ }
48
+
49
+ ```