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

質問編集履歴

2

インデックスの入力と配列への入力を別にしました。

2019/07/05 05:53

投稿

NIKONIKONIKONIK
NIKONIKONIKONIK

スコア83

title CHANGED
File without changes
body CHANGED
@@ -12,8 +12,8 @@
12
12
  Input Mismatch
13
13
 
14
14
 
15
-
15
+ ```before
16
- ```import java.util.InputMismatchException;
16
+ import java.util.InputMismatchException;
17
17
  import java.util.Scanner;
18
18
  public class Kadai1104 {
19
19
 
@@ -44,6 +44,42 @@
44
44
  }
45
45
  }
46
46
  }
47
+ ```after
47
48
 
48
- コード
49
+ ```
50
+
51
+ import java.util.InputMismatchException;
52
+ import java.util.Scanner;
53
+ public class Kadai1104 {
54
+ public static void main(String[] args) {
55
+ Scanner sc = new Scanner(System.in);
56
+ int[] nums = {-1,-1,-1,-1,-1};
57
+ int index = 0;
58
+ do {
59
+ try {
60
+ System.out.print("Index --> ");
61
+ index = sc.nextInt();
62
+ }catch(ArrayIndexOutOfBoundsException e) {
63
+ System.out.println("Index Error!");
64
+ System.out.println();
65
+ }
66
+ }while(index != -1);
67
+ do {
68
+ try {
69
+ System.out.print("Nums[" + index + "] --> ");
70
+ nums[index] = sc.nextInt();
71
+ }catch(InputMismatchException e){
72
+ System.out.println("Input Mismatch");
73
+ sc.next();
74
+ System.out.println();
75
+ }
76
+ }while(index != -1);
77
+
78
+
79
+ System.out.println();
80
+ for(int i=0; i<nums.length; i++) {
81
+ System.out.println("Nums[" + i + "] : " + nums[i]);
82
+ }
83
+ }
84
+ }
49
85
  ```

1

変更していません

2019/07/05 05:53

投稿

NIKONIKONIKONIK
NIKONIKONIKONIK

スコア83

title CHANGED
File without changes
body CHANGED
@@ -2,8 +2,17 @@
2
2
 
3
3
  String型で入力された際に例外処理でint型で入力してくださいとエラーを出すのですが、その際に配列の添え字を変えずにそのまま再入力していく方法がわかりません。
4
4
 
5
+ 下記の例のようにNums[2]を変えずに再入力をしたい
5
6
  例: Index --> 2
7
+ Nums[2] --> a
8
+ Input Mismatch
9
+ Nums[2] --> b
10
+ Input Mismatch
11
+ Nums[2] --> c
12
+ Input Mismatch
6
13
 
14
+
15
+
7
16
  ```import java.util.InputMismatchException;
8
17
  import java.util.Scanner;
9
18
  public class Kadai1104 {