Welcome to JAVA!(第4章课后习题)
2015-10-07 21:12
453 查看
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.
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.
4.19**Write a nested for loop that prints the following output:
运行结果:
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.
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 *///~
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树
- [原创]java局域网聊天系统