import java.util.Scanner;
public class GetSum
{
public static void main(String[] args)
{
GetSum gs = new GetSum();
gs.getSum();
}
public void getSum(){
Scanner input = new Scanner(System.in);
System.out.print("要输出1到n的整数和,请输入n:");
int num = input.nextInt();
int sum = 0;
for ( int i = 1; i <= num ; i++ )
{
sum += i;
}
System.out.println("1到" + num + "的整数和为:" + sum );
}
}
与
import java.util.Scanner;
public class GetSum
{
public static void main(String[] args)
{
getSum();
}
public static void getSum(){
Scanner input = new Scanner(System.in);
System.out.print("要输出1到n的整数和,请输入n:");
int num = input.nextInt();
int sum = 0;
for ( int i = 1; i <= num ; i++ )
{
sum += i;
}
System.out.println("1到" + num + "的整数和为:" + sum );
}
}
我也知道静态方法只能静态方法中调用 , 但想知道这两者的分别和优缺!是那一个比较实e用点。 |
|