回答編集履歴

2

空行の削除

2022/06/18 09:27

投稿

kazuma-s
kazuma-s

スコア8224

test CHANGED
@@ -44,7 +44,6 @@
44
44
  result += Integer.valueOf(str);
45
45
  }
46
46
  System.out.println(result);
47
-
48
47
  }
49
48
  public static void main(String[] args) {
50
49
  cal app = new cal();

1

追記

2022/06/18 09:25

投稿

kazuma-s
kazuma-s

スコア8224

test CHANGED
@@ -26,4 +26,34 @@
26
26
  }
27
27
  }
28
28
  ```
29
+ **追記**
30
+ 質問のコードに近いものにしてみました。
31
+ ・main でループする。
32
+ ・equals ではなく、compareTo を使う。
33
+ ・parseInt ではなく、valueOf を使う。
34
+ ```Java
35
+ import java.util.*;
29
36
 
37
+ class cal {
38
+ private void processInput(Scanner scan) {
39
+ int result = 0;
40
+ while (true) {
41
+ String str = scan.next();
42
+ if (str.compareTo("quit") == 0) System.exit(0);
43
+ if (str.compareTo("end") == 0) break;
44
+ result += Integer.valueOf(str);
45
+ }
46
+ System.out.println(result);
47
+
48
+ }
49
+ public static void main(String[] args) {
50
+ cal app = new cal();
51
+ Scanner scan = new Scanner(System.in);
52
+ while (true) {
53
+ System.out.println("Please input numbers: ");
54
+ app.processInput(scan);
55
+ }
56
+ }
57
+ }
58
+ ```
59
+