回答編集履歴

1

コード修正

2020/08/29 22:13

投稿

kazuma-s
kazuma-s

スコア8224

test CHANGED
@@ -88,69 +88,69 @@
88
88
 
89
89
  import java.util.Scanner;
90
90
 
91
- import java.util.regex.MatchResult;
91
+ import java.util.regex.*; // Pattern, MatchResult;
92
92
 
93
93
 
94
94
 
95
95
  class Test {
96
96
 
97
- public static void main(String[] args) {
97
+ public static void main(String[] args) {
98
98
 
99
- Scanner sc = new Scanner(System.in);
99
+ Scanner sc = new Scanner(System.in);
100
100
 
101
- while (true) {
101
+ Pattern pat = Pattern.compile("(\d+)\s*(.)\s*(\d+)\s*=");
102
102
 
103
- System.out.print(">> ");
103
+ while (true) {
104
104
 
105
- if (!sc.hasNextLine()) break;
105
+ System.out.print(">> ");
106
106
 
107
- String line = sc.nextLine();
107
+ if (!sc.hasNextLine()) break;
108
108
 
109
- Scanner sc2 = new Scanner(line);
109
+ String line = sc.nextLine();
110
110
 
111
- try {
111
+ Scanner sc2 = new Scanner(line);
112
112
 
113
- sc2.findInLine("(\d+)\s*(.)\s*(\d+)\s*=");
113
+ try {
114
114
 
115
- MatchResult r = sc2.match();
115
+ sc2.findInLine(pat);
116
116
 
117
- if (r.groupCount() != 3) throw new Exception();
117
+ MatchResult r = sc2.match();
118
118
 
119
- int x = Integer.valueOf(r.group(1));
119
+ int x = Integer.parseInt(r.group(1));
120
120
 
121
- int y = Integer.valueOf(r.group(3));
121
+ int y = Integer.parseInt(r.group(3));
122
122
 
123
- int z = 0;
123
+ int z = 0;
124
124
 
125
- switch (r.group(2)) {
125
+ switch (r.group(2)) {
126
126
 
127
- case "+": z = x + y; break;
127
+ case "+": z = x + y; break;
128
128
 
129
- case "-": z = x - y; break;
129
+ case "-": z = x - y; break;
130
130
 
131
- case "*": z = x * y; break;
131
+ case "*": z = x * y; break;
132
132
 
133
- case "/": z = x / y; break;
133
+ case "/": z = x / y; break;
134
134
 
135
- }
135
+ }
136
136
 
137
- System.out.println(z);
137
+ System.out.println(z);
138
138
 
139
- }
139
+ }
140
140
 
141
- catch (Exception e) {
141
+ catch (Exception e) {
142
142
 
143
- System.out.println(" Error");
143
+ System.out.println(" Error");
144
144
 
145
- }
145
+ }
146
146
 
147
- sc2.close();
147
+ sc2.close();
148
148
 
149
- }
149
+ }
150
150
 
151
- sc.close();
151
+ sc.close();
152
152
 
153
- }
153
+ }
154
154
 
155
155
  }
156
156