日期:2014-05-20 浏览次数:20857 次
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public int Sum(int n) { int sum = 0; for (int i = 0; i < n + 1; i++) { sum = sum + i; } return sum; } public static void main(String[] args) { Main problem = new Main(); int a[] = new int[100], m = 0, i = 0; String strInt = null; BufferedReader reader = new BufferedReader(new InputStreamReader( System.in)); while (true) { try { strInt = reader.readLine(); if (strInt.matches("^[1-9][0-9]+$")) {//不退出怎么行。这样的结果你满意吗? a[i++] = Integer.parseInt(strInt); break; } else { System.out.println("输入数据无效,请重新输入"); } } catch (NumberFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } while (a[m] != 0) { System.out.println(problem.Sum(a[m++])); System.out.print("\n"); } } } 运行结果: 100 5050
------解决方案--------------------
好难哦
------解决方案--------------------
那楼主还有啥问题啊,感觉没什么问题了,多加点约束条件撒