teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

ちょっとだけリファクタ

2018/04/18 13:31

投稿

LouiS0616
LouiS0616

スコア35678

answer CHANGED
@@ -1,20 +1,19 @@
1
1
  こんなのはどうでしょう。
2
2
  ```Java
3
- import java.util.*;
3
+ import java.util.Scanner;
4
4
 
5
5
  class Main {
6
6
  static int inputInt(Scanner sc, String message) {
7
7
  while(true) {
8
8
  try {
9
- return sc.nextInt();
9
+ return Integer.parseInt(sc.nextLine());
10
10
  }
11
- catch(InputMismatchException e) {
11
+ catch(NumberFormatException e) {
12
- sc.nextLine();
13
12
  System.out.print(message);
14
13
  }
15
14
  }
16
15
  }
17
-
16
+
18
17
  public static void main(String[] args) {
19
18
  try(Scanner sc = new Scanner(System.in)) {
20
19
  int playCount = -1;

1

なんとなくメソッド名が気に入らなかった

2018/04/18 13:31

投稿

LouiS0616
LouiS0616

スコア35678

answer CHANGED
@@ -3,7 +3,7 @@
3
3
  import java.util.*;
4
4
 
5
5
  class Main {
6
- static int scanInt(Scanner sc, String message) {
6
+ static int inputInt(Scanner sc, String message) {
7
7
  while(true) {
8
8
  try {
9
9
  return sc.nextInt();
@@ -22,7 +22,7 @@
22
22
  String message = "ゲーム回数は1回以上です。\nゲーム回数を入力してください\n";
23
23
 
24
24
  System.out.print(message);
25
- playCount = scanInt(sc, message);
25
+ playCount = inputInt(sc, message);
26
26
  }
27
27
 
28
28
  System.out.println("playCountは" + playCount + "です。");