挣分,挣分
- package exam29;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.text.DecimalFormat;
- public class Exam26 {
- public static void main(String[] args) throws IOException {
- System.out.println("请输入整数:");
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String line = null;
- while((line=br.readLine())!=null){
- if("exit".equals(line)){
- System.out.println("程序结束");
- break;
- }
- int n=Integer.parseInt(line);//n为输入的数字
- double count=0;
- if(n%2==0){
- System.out.print("该数是偶数,");
- double temp= n/2;
- for( double i=1;i<=temp;i++){
- count += 1/(i*2);
- }
- }else{
- System.out.print("该数是奇数,");
- double temp=(n+1)/2;
- for(double i=1;i<=temp;i++){
- count +=1/(2*i-1);
- }
- }
- DecimalFormat df = new DecimalFormat("###.00000000");
- System.out.println("保留小数点后8位="+df.format(count));
- }
- }
- }
复制代码
|
-
1.png
(10.31 KB, 下载次数: 8)
|