質問編集履歴

1

コードの修正

2020/06/01 02:43

投稿

PC_breakman
PC_breakman

スコア30

test CHANGED
File without changes
test CHANGED
@@ -2,9 +2,7 @@
2
2
 
3
3
 
4
4
 
5
- AOJの[こちら](http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0011)の値をスワップしてあみだくじを解くプログラムをJavaで作るために以下のようなコードを書きましたが、オンラインジャッジ上でRuntime Errorになってしまいました。どのように対処たらのかわからないので教えていただけたら幸いでございます
6
-
7
- 何卒よろしくお願いいたします。
5
+ AOJの[こちら](http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0011)の値をスワップしてあみだくじを解くプログラムをJavaで作るために以下のようなコードを書きましたが、オンラインジャッジ上でRuntime Errorになってしまいました。カンマの入力を受け入れてくれないのかと思います。どのように直せばよろしいのでしょうか。
8
6
 
9
7
 
10
8
 
@@ -20,49 +18,39 @@
20
18
 
21
19
  public class Main {
22
20
 
23
- public static void swap(int x, int y){ //現在の縦棒の数値を入れ替える。
24
-
25
- int z;
26
-
27
- z = x;
28
-
29
- x = y;
30
-
31
- y = z;
32
-
33
- }
34
-
35
21
  public static void main(String[] args){
36
22
 
37
23
  Scanner sc = new Scanner(System.in);
38
24
 
39
- int w = sc.nextInt(); //縦棒の本数
25
+ int w = sc.nextInt();
40
26
 
41
- int n = sc.nextInt(); //入れ替え回数
42
-
43
- int[] a = new int[n]; //入れ替える縦棒の番号。
44
-
45
- int[] b = new int[n];
27
+ int n = sc.nextInt();
46
28
 
47
29
  int[] stick = new int[w];
48
30
 
49
- for(int i = 0; i < n; i++){
50
-
51
- a[i] = sc.nextInt();
52
-
53
- b[i] = sc.nextInt();
54
-
55
- }
56
-
57
31
  for(int i = 0; i < w; i++){
58
32
 
59
- stick[i] = i + 1; //それぞれの縦棒の数値に初期値を代入
33
+ stick[i] = i + 1;
60
34
 
61
35
  }
62
36
 
63
37
  for(int i = 0; i < n; i++){
64
38
 
39
+ String input = sc.nextLine();
40
+
41
+ String[] aSt = input.split(",",0);
42
+
43
+ int[] a = new int[2];
44
+
45
+ a[0] = Integer.parseInt(aSt[0]);
46
+
47
+ a[1] = Integer.parseInt(aSt[1]);
48
+
49
+ int sw = stick[a[0] - 1];
50
+
65
- swap(stick[a[i]],stick[b[i]]);
51
+ stick[a[0] - 1] = stick[a[1] - 1];
52
+
53
+ stick[a[1] - 1] = sw;
66
54
 
67
55
  }
68
56
 
@@ -86,7 +74,11 @@
86
74
 
87
75
 
88
76
 
77
+ 19行目から21行目にかけての配列外参照を修正しました。
78
+
79
+ また、swapを関数を使わずに書きました。
80
+
89
- 配列配列というのが何とくまずかないましたが、これ以外思つきせんでした。
81
+ カンマ入力も受け入れてもらえる方法を調べて書きましたが、コンパイラ整数値でないといわれてしまいました。
90
82
 
91
83
 
92
84