用JAVA编程求sum=2+5+8+11+14+…,输入正整数n,求sum的前n项和。
展开全部
public
static
void
main(String[]
args)
{
BufferedReader
br=new
BufferedReader(new
InputStreamReader(System.in));
System.out.println("请输入一个正整数n:
");
try
{
int
n=Integer.parseInt(br.readLine());
double
sum=0;
sum=2*n+3*(n*n-n)/2;
System.out.println("sum的前n项和为:
"+sum);
}
catch
(NumberFormatException
e)
{
e.printStackTrace();
}
catch
(IOException
e)
{
e.printStackTrace();
}
}
static
void
main(String[]
args)
{
BufferedReader
br=new
BufferedReader(new
InputStreamReader(System.in));
System.out.println("请输入一个正整数n:
");
try
{
int
n=Integer.parseInt(br.readLine());
double
sum=0;
sum=2*n+3*(n*n-n)/2;
System.out.println("sum的前n项和为:
"+sum);
}
catch
(NumberFormatException
e)
{
e.printStackTrace();
}
catch
(IOException
e)
{
e.printStackTrace();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询