这题我不会
展开全部
import javax.swing.JOptionPane;
public class function1
{
public static void main(String[ ] args)
{
double y = 0 ;
int n , i = 1 ;
String n_String;
n_String = JOptionPane. showInputDialog("输入1+1/2+1/3+…+1/n 之n值");
n = Integer.parseInt( n_String );
while( i <= n ){
y = y + 1.0 / i ;
i = i + 1;
}
JOptionPane.showMessageDialog (null,"f("+n+")=1+1/2+…+1/"+n+"="+y,"求函数", JOptionPane.INFORMATION_ MESSAGE);
}
}
编译通过的while循环
for循环
import javax.swing.JOptionPane;
public class function1_for
{
public static void main(String[ ] args)
{
double y = 0 ;
int n , i ;
String n_String;
n_String = JOptionPane. showInputDialog("输入1+1/2+1/3+…+1/n 之n值");
n = Integer.parseInt( n_String );
for( i = 1 ; i <= n ; i++ )
y = y + 1.0 / i ;
JOptionPane.showMessageDialog.
}
}
do循环
import javax.swing.JOptionPane;
public class function1_do
{
public static void main(String[ ] args)
{
double y = 0 ;
int n , i = 0 ;
String n_String;
n_String = JOptionPane. showInputDialog("输入1+1/2+1/3+…+1/n 之n值");
n = Integer.parseInt( n_String );
do{
i++ ;
y = y + 1.0 / i ;
}while( i <n );
JOptionPane.showMessageDialog ( null,"f(" + n + ") = 1+1/2+…+1/" + n +"=" + y,"求函数", JOptionPane. INFORMATION_MESSAGE);
}
}当n很大时):
1+1/2+1/3+......+1/n≈lnn+C(C=0.57722......一个无理数,称作欧拉初始,专为调和级数所用)
public class function1
{
public static void main(String[ ] args)
{
double y = 0 ;
int n , i = 1 ;
String n_String;
n_String = JOptionPane. showInputDialog("输入1+1/2+1/3+…+1/n 之n值");
n = Integer.parseInt( n_String );
while( i <= n ){
y = y + 1.0 / i ;
i = i + 1;
}
JOptionPane.showMessageDialog (null,"f("+n+")=1+1/2+…+1/"+n+"="+y,"求函数", JOptionPane.INFORMATION_ MESSAGE);
}
}
编译通过的while循环
for循环
import javax.swing.JOptionPane;
public class function1_for
{
public static void main(String[ ] args)
{
double y = 0 ;
int n , i ;
String n_String;
n_String = JOptionPane. showInputDialog("输入1+1/2+1/3+…+1/n 之n值");
n = Integer.parseInt( n_String );
for( i = 1 ; i <= n ; i++ )
y = y + 1.0 / i ;
JOptionPane.showMessageDialog.
}
}
do循环
import javax.swing.JOptionPane;
public class function1_do
{
public static void main(String[ ] args)
{
double y = 0 ;
int n , i = 0 ;
String n_String;
n_String = JOptionPane. showInputDialog("输入1+1/2+1/3+…+1/n 之n值");
n = Integer.parseInt( n_String );
do{
i++ ;
y = y + 1.0 / i ;
}while( i <n );
JOptionPane.showMessageDialog ( null,"f(" + n + ") = 1+1/2+…+1/" + n +"=" + y,"求函数", JOptionPane. INFORMATION_MESSAGE);
}
}当n很大时):
1+1/2+1/3+......+1/n≈lnn+C(C=0.57722......一个无理数,称作欧拉初始,专为调和级数所用)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询