質問編集履歴

5

コードの修正

2023/11/30 05:57

投稿

zes
zes

スコア1

test CHANGED
File without changes
test CHANGED
@@ -10,6 +10,8 @@
10
10
  2と入力したら中国、1と入力したらアメリカなど対応する名前を表示したいです。
11
11
  -1を入力したら終わるループ文で回したいのですがどのような条件式で出来ますでしょうか。
12
12
 
13
+ 配列外の数字を入力したらErrorと表示させたいのですが、java.lang.ArrayIndexOutOfBoundsExceptionとエラーメッセージが表示されてどのように対策したらいいか分からない。
14
+
13
15
  ### 試したこと
14
16
 
15
17
  indexofやequalsなどのメソッドを使うのか考えたがwhile文などの条件式での使い方が分からなかった。
@@ -18,7 +20,7 @@
18
20
 
19
21
  ```Java
20
22
  import java.util.Scanner;
21
- public class co {
23
+ public class Kadai0701 {
22
24
  public static void main(String[] args) {
23
25
  Scanner sc = new Scanner(System.in);
24
26
 
@@ -26,15 +28,17 @@
26
28
 
27
29
  System.out.print("Index(-1:End)-->");
28
30
  int num = sc.nextInt();
31
+
29
32
  while(num != -1) {
30
-
33
+ if(num > 4) {
31
- for(int i = 0;i < os.length;i++) {
34
+ System.out.println("Index Error!");
35
+ }
32
- System.out.println("外国 Name : " + co[i]);
36
+ System.out.println("外国 Name : " + co[num]);
33
-
34
37
  System.out.print("Index(-1:End)-->");
35
38
  num = sc.nextInt();
36
- }
37
39
  }
40
+ sc.close();
38
41
  }
42
+
39
43
  }
40
44
  ```

4

誤字

2023/11/29 22:18

投稿

zes
zes

スコア1

test CHANGED
File without changes
test CHANGED
@@ -26,7 +26,7 @@
26
26
 
27
27
  System.out.print("Index(-1:End)-->");
28
28
  int num = sc.nextInt();
29
- while(num == -1) {
29
+ while(num != -1) {
30
30
 
31
31
  for(int i = 0;i < os.length;i++) {
32
32
  System.out.println("外国 Name : " + co[i]);

3

誤字

2023/11/29 21:46

投稿

zes
zes

スコア1

test CHANGED
File without changes
test CHANGED
@@ -29,7 +29,7 @@
29
29
  while(num == -1) {
30
30
 
31
31
  for(int i = 0;i < os.length;i++) {
32
- System.out.println("外国 Name : " + os[i]);
32
+ System.out.println("外国 Name : " + co[i]);
33
33
 
34
34
  System.out.print("Index(-1:End)-->");
35
35
  num = sc.nextInt();

2

ソースコードの追加

2023/11/29 21:46

投稿

zes
zes

スコア1

test CHANGED
File without changes
test CHANGED
@@ -13,3 +13,28 @@
13
13
  ### 試したこと
14
14
 
15
15
  indexofやequalsなどのメソッドを使うのか考えたがwhile文などの条件式での使い方が分からなかった。
16
+
17
+ ### 該当のソースコード
18
+
19
+ ```Java
20
+ import java.util.Scanner;
21
+ public class co {
22
+ public static void main(String[] args) {
23
+ Scanner sc = new Scanner(System.in);
24
+
25
+ String[] co = {"日本","アメリカ","中国","イギリス","ロシア"};
26
+
27
+ System.out.print("Index(-1:End)-->");
28
+ int num = sc.nextInt();
29
+ while(num == -1) {
30
+
31
+ for(int i = 0;i < os.length;i++) {
32
+ System.out.println("外国 Name : " + os[i]);
33
+
34
+ System.out.print("Index(-1:End)-->");
35
+ num = sc.nextInt();
36
+ }
37
+ }
38
+ }
39
+ }
40
+ ```

1

書式の改善

2023/11/29 21:27

投稿

zes
zes

スコア1

test CHANGED
File without changes
test CHANGED
@@ -1,13 +1,15 @@
1
1
  ### 実現したいこと
2
2
 
3
3
 
4
- 添字番号を入力したらそれに対応する名前を出たい。
4
+ Javaで添字番号を入力したらそれに対応する名前を出すプログラムを作りたい。
5
5
  特定の数字を入力し、終了するまでループ文で回す。
6
6
 
7
7
  ### 前提
8
8
 
9
- Stringの配列で[日本,アメリカ,中国,イギリス,ロシア]などの文字を初期値として配列を作り、
9
+ Stringの配列で String[] os = ["日本","アメリカ","中国","イギリス","ロシア"]などの文字を初期値として配列を作り、
10
10
  2と入力したら中国、1と入力したらアメリカなど対応する名前を表示したいです。
11
11
  -1を入力したら終わるループ文で回したいのですがどのような条件式で出来ますでしょうか。
12
12
 
13
+ ### 試したこと
13
14
 
15
+ indexofやequalsなどのメソッドを使うのか考えたがwhile文などの条件式での使い方が分からなかった。