Welcome to JAVA!(제4 장 방과 후 연습 문제)

3771 단어 JAVA 학습 노트
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

*///~

좋은 웹페이지 즐겨찾기