teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

ご回答を受けて、試したことを追記

2017/12/18 05:15

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -61,4 +61,45 @@
61
61
  Eclipse Java EE IDE for Web Developers.
62
62
 
63
63
  Version: Neon.3 Release (4.6.3)
64
- Build id: 20170314-1500
64
+ Build id: 20170314-1500
65
+
66
+ ###試したこと
67
+ ```java
68
+ import java.util.Scanner;
69
+
70
+ public class trial12_14 {
71
+
72
+ public static void main(String[] args) {
73
+ Scanner stdIn = new Scanner(System.in);
74
+ System.out.println("2つの整数の和を求めます。");
75
+
76
+ System.out.println("aの値:"); String a = stdIn.next();
77
+ System.out.println("bの値:"); String b = stdIn.next();
78
+ System.out.println(a + "+" + b);
79
+
80
+ int a_dec = Integer.decode(a);
81
+ int b_dec = Integer.decode(b);
82
+ int result_dec = a_dec + b_dec;
83
+
84
+ String result_hex = Integer.toHexString(result_dec);
85
+ System.out.print(result_hex);
86
+ System.out.print("(" + result_dec + ")");
87
+ }
88
+ }
89
+ ```
90
+ ご回答を受けて上記のように書き換えたところ、
91
+ ```
92
+ 2つの整数の和を求めます。
93
+ aの値:
94
+ ab
95
+ bの値:
96
+ ab
97
+ ab+ab
98
+ Exception in thread "main" java.lang.NumberFormatException: For input string: "ab"
99
+ at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
100
+ at java.lang.Integer.parseInt(Integer.java:580)
101
+ at java.lang.Integer.valueOf(Integer.java:740)
102
+ at java.lang.Integer.decode(Integer.java:1197)
103
+ at javaalgorithm.trial_16sum.main(trial_16sum.java:15)
104
+ ```
105
+ とエラーが表示されてしまいました。

1

コードのコメントを修正

2017/12/18 05:15

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -29,7 +29,7 @@
29
29
  public static void main(String[] args) {
30
30
  Scanner stdIn = new Scanner(System.in);
31
31
  System.out.println("2つの整数の和を求めます。");
32
- //入力は16進数でも10進数でも認め
32
+ //入力はa,b共に16進数かa,b共に10進数のどちらかと仮定す
33
33
  System.out.println("aの値:"); String a = stdIn.next();
34
34
  System.out.println("bの値:"); String b = stdIn.next();
35
35
  System.out.println(a + "+" + b);