質問編集履歴
1
コードの修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
### 質問内容
|
2
2
|
|
3
|
-
AOJの[こちら](http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0011)の値をスワップしてあみだくじを解くプログラムをJavaで作るために以下のようなコードを書きましたが、オンラインジャッジ上でRuntime Errorになってしまいました。どのように
|
4
|
-
何卒よろしくお願いいたします。
|
3
|
+
AOJの[こちら](http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0011)の値をスワップしてあみだくじを解くプログラムをJavaで作るために以下のようなコードを書きましたが、オンラインジャッジ上でRuntime Errorになってしまいました。カンマの入力を受け入れてくれないのかと思います。どのように直せばよろしいのでしょうか。
|
5
4
|
|
6
5
|
### 該当のソースコード
|
7
6
|
|
@@ -9,28 +8,23 @@
|
|
9
8
|
import java.util.Scanner;
|
10
9
|
|
11
10
|
public class Main {
|
12
|
-
public static void swap(int x, int y){ //現在の縦棒の数値を入れ替える。
|
13
|
-
int z;
|
14
|
-
z = x;
|
15
|
-
x = y;
|
16
|
-
y = z;
|
17
|
-
}
|
18
11
|
public static void main(String[] args){
|
19
12
|
Scanner sc = new Scanner(System.in);
|
20
|
-
int w = sc.nextInt(); //縦棒の本数
|
21
|
-
int n = sc.nextInt(); //入れ替え回数
|
22
|
-
int[] a = new int[n]; //入れ替える縦棒の番号。
|
23
|
-
int
|
13
|
+
int w = sc.nextInt();
|
14
|
+
int n = sc.nextInt();
|
24
15
|
int[] stick = new int[w];
|
25
|
-
for(int i = 0; i < n; i++){
|
26
|
-
a[i] = sc.nextInt();
|
27
|
-
b[i] = sc.nextInt();
|
28
|
-
}
|
29
16
|
for(int i = 0; i < w; i++){
|
30
|
-
stick[i] = i + 1;
|
17
|
+
stick[i] = i + 1;
|
31
18
|
}
|
32
19
|
for(int i = 0; i < n; i++){
|
20
|
+
String input = sc.nextLine();
|
21
|
+
String[] aSt = input.split(",",0);
|
22
|
+
int[] a = new int[2];
|
23
|
+
a[0] = Integer.parseInt(aSt[0]);
|
24
|
+
a[1] = Integer.parseInt(aSt[1]);
|
25
|
+
int sw = stick[a[0] - 1];
|
33
|
-
|
26
|
+
stick[a[0] - 1] = stick[a[1] - 1];
|
27
|
+
stick[a[1] - 1] = sw;
|
34
28
|
}
|
35
29
|
for(int i = 0; i < w; i++){
|
36
30
|
System.out.println(stick[i]);
|
@@ -42,7 +36,9 @@
|
|
42
36
|
|
43
37
|
### 試したこと
|
44
38
|
|
39
|
+
19行目から21行目にかけての配列外参照を修正しました。
|
40
|
+
また、swapを関数を使わずに書きました。
|
45
|
-
|
41
|
+
カンマの入力も受け入れてもらえる方法を調べて書きましたが、コンパイラに整数値でないといわれてしまいました。
|
46
42
|
|
47
43
|
### 補足情報(FW/ツールのバージョンなど)
|
48
44
|
|