本帖最后由 十万一千 于 2014-11-19 16:25 编辑
- public static void main(String[] args) {
- try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));) {
- System.out.println("请输入[1-9]的整数:");
- Integer max = new Integer(bufferedReader.readLine());
- if (max >= 1 && max <= 9) {
- Integer sum = 0;
- StringBuilder stringBuilder = new StringBuilder();
- String baseNumber = "2";
- for (int i = 0; i < max; i++) {
- Integer tmp = new Integer(stringBuilder.append(baseNumber).toString());
- if (i == max - 1) {
- System.out.print(tmp + "=");
- } else {
- System.out.print(tmp + "+");
- }
- sum += tmp;
- }
- System.out.println(sum);
- } else {
- throw new RuntimeException("输入数值过大");
- }
- } catch (NumberFormatException | IOException e) {
- System.out.println("输入错误!");
- }
- }
复制代码
|