i>1 时
计算m(i)=1/2+2/3+.......+(i-1)/i
要求输出下表:
i
m(i)
2
0.5
3
1.1667
....
....
19
15.4523
20
16.4023
我写的程序 :
import javax.swing.JOptionPane;
public class Main {
public static void main(String[] args) {
String numString = JOptionPane.showInputDialog(null,
"Enter an index for thenumber:","Ex4.6",JOptionPane.QUESTION_MESSAGE);
double i=Double.parseDouble(numString);
if(i<=1)
JOptionPane.showMessageDialog(null,"the value you enter is wrong,the number must be bigger than 1",
"Ex4.6",JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageDialog(null,"the m(i) is"+factorial(i-1),
"Ex4.6",JOptionPane.INFORMATION_MESSAGE);
}
static double factorial(double i) {
double a = 1/2d+2/3d;
if(i==2)
return a;
else
return i/(i+1)+factorial(i-1);
}
}
我计算出了m(i),但是就是不知道怎么才能按要求的那样输出,请指教啊~~最好在我程序的基础上修改吧~~谢谢谢谢~~
|