Welcome to JAVA!(第4章课后习题)

4.1* Write a program that reads an unspecified number of integers,determines how many postive and negative values have been read,and computes the total and average of the input values (not conuting zeros).Your program ends with the input 0,Display the average as a floating-point number.

import java.util.*;
public class Main {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int sum=0,x,num1=0,num2=0;
		while (true)
		{
			x=input.nextInt();
			if (x == 0)
			{
				break;
			}
			if (x>0)
				num1++;
			else 
				num2++;
			sum = sum + x;
		}
		System.out.println("The number of Positives is " + num1 );
		System.out.println("The number of negatives is " + num2 );
		System.out.println("The total is " + sum );
		System.out.println("The average is " + (float)(sum*1.0f/(num1+num2)));
	}

}
/* output:
1 2 -1 3 0
The number of Positives is 3
The number of negatives is 1
The total is 5
The average is 1.25
*///~


4.16**Write a program that reads an integer and displays all its smallest factors in increasing order.For example, if the input integer is 120,the output should be as follows:2,2,2,3,5.

import java.util.*;
public class Main {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int num=0,i=2;
		num=input.nextInt();
		System.out.print("The The smallest factor is ");
		while (num!=0)
		{
			if (num%i==0)
			{
				System.out.print(i + " ");
				num/=i;
			}
			if (num%i!=0)
			{
				i++;
			}
		}
	
	}

}
/* output:
120
The The smallest factor is 2 2 2 3 5 
*///~


4.19**Write a nested for loop that prints the following output:

import java.util.*;
import java.util.Scanner;  
public class Main {  
    public static void main(String[] args) {  
    	int i,j,num=1;
    	for ( i=0; i<8 ; i++ )
    	{
    		for( j=7-i; j>0; j--)
    		{
    			System.out.print("\t");
    		}
    		for(j=1;j<=num;j*=2)  
        	{  
        	 	System.out.print(j);  
       		 	System.out.print('\t');  
       	 	}  
       	 	for(j=num/2;j>=1;j/=2)  
       	 	{  
       	 		System.out.print(j);  
       	 		System.out.print('\t');  
       	 	}      	
        	System.out.println();  
            num*=2;  
         }  
     }  
}
    

运行结果:

/*output:
 * 							1	
						1	2	1	
					1	2	4	2	1	
				1	2	4	8	4	2	1	
			1	2	4	8	16	8	4	2	1	
		1	2	4	8	16	32	16	8	4	2	1	
	1	2	4	8	16	32	64	32	16	8	4	2	1	
1	2	4	8	16	32	64	128	64	32	16	8	4	2	1	

 *///~

4.25**You Can approximate e using the following series:e=1+1/1!+1/2!+1/3!+1/4!+···+1/i!.Write a program that displays the e value for i = 10000, 20000 , ··· , and 100000.

public class Main 
{  
    public static void main(String[] args)  
    {  
        int n=10000,s=1,j;  
        double Pi,i;  
        for(j=n; j<=100000; j+=10000)  
        {  
            Pi=0;  
            for(i=1; i<=n; i+=2)  
            {  
                Pi+=(s*1/i);  
                s*=-1;  
            }  
            Pi*=4.0;  
            System.out.println("The π value of "+ j +" is: "+ Pi );    
        }  
    }  
}  
运行结果:
/*output:
 * The π value of 10000 is: 3.141392653591791
The π value of 20000 is: 3.141392653591791
The π value of 30000 is: 3.141392653591791
The π value of 40000 is: 3.141392653591791
The π value of 50000 is: 3.141392653591791
The π value of 60000 is: 3.141392653591791
The π value of 70000 is: 3.141392653591791
The π value of 80000 is: 3.141392653591791
The π value of 90000 is: 3.141392653591791
The π value of 100000 is: 3.141392653591791

*///~






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值