我有一个在 Java 中添加 2 个数字并输出总和的程序。(数字通过键盘输入)。但是,我有一个需要帮助解决的错误。我将在代码之后解释错误:
package com.sigma.java7;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Addition {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
do {
try {
System.out.println("Enter A :");
String numA = br.readLine();
int a = Integer.parseInt(numA);
break;
} catch (Exception e) {
System.out.println("Incorrect input. Please enter an integer.");
}
} while (true);
do {
try {
System.out.println("Enter B :");
String numB = br.readLine();
int b = Integer.parseInt(numB);
break;
} catch (Exception e) {
System.out.println("Incorrect input. Please enter an integer.");
}
} while (true);
System.out.println("The sum of the numbers is: " +(a+b));
br.close();
}
}
在行
"System.out.println("The sum of the numbers is: " +(a+b));"
我收到错误消息:“a 无法解析为变量,b 无法解析为变量。” 为什么?