题目要求如该网址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");
}
}}
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");
}
}}
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
import java.util.Scanner;
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 another = new Main();
Scanner scanner = new Scanner(System.in);
int a[]=new int[10];
while (scanner.hasNext()) {
int i=0,j=0;
a[i++]=scanner.nextInt();
while (a[j]!=0) {
System.out.println(another.Sum(a[j++])+"\n");
}
}
}}