题目要求如该网址http://acm.hdu.edu.cn/showproblem.php?pid=1001。请问我这样做有什么问题
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.length()==0) {
break;
}
else {
a[i++]=Integer.parseInt(strInt);
}
} 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");
}
}
}
------解决方案--------------------
- Java code
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"); } }}运行结果:1005050
------解决方案--------------------
好难哦
------解决方案--------------------
那楼主还有啥问题啊,感觉没什么问题了,多加点约束条件撒