第四章习题总结

第四章循环语句习题总结:
**例题1:**找出整数的所有最小因子,以升序排列,例如:输入120, 显示2,2,2,3,5;
思路分析:因为1可以被任何数整除,所以从2开始,依次对3,4,5,6…进行判断;
先对2进行取余,若(number%20),则输出2;
此时number变为number/2;
再继续返回第一步循环;
若(number%2!=0),再对3取余…继续循环;
程序如下:
//1.提示用户输入整数number
//2.number依次对大于2的整数取余
//3.判断后输出
public class test4_5问题 {
public static void main(String[] args){
Scanner scanner=new Scanner(System.out.println);
System.out.println(“enter the number”); / /1.提示用户输入整数number;
int number=scanner.nextInt();
while(true){
boolean flag=true;
for(int i=2;i<=number/2;i++){
if(number%i
0){ //2.number依次对大于2的整数取余
System.out.println(i);
number=number/i;
flag=false;
break; ////////////有问题//3.判断后输出
}
}
if(flag){
continue; /////////////
}else{
System.out.println(number);/////////
break;
}
}
}
}
这里看到用到continue,还有flag做标记,没太明白什么意思,自己编程时就是这里遇到瓶颈。
**例题2:*求数列和 1/3+3/5+5/7+7/9+9/11+…+97/99;
解:应该用for循环来写相加之和,可以看出数列的每一项都是有规律的,若用n表示项数,则数列的第n项应表示为:
(2n-1)/(2n+1)
则程序如下:
public static void main(String[] args){
double sum=0;
for(int n=1;n<=49;n++){ / / / /因为2n-1=97;n=49
**sum+=( (2
n-1) / (2*n+1) )1.0;
/ / / / 乘以1.0将n转换为浮点型
}
System.out.println(sum);
}
例题3
求e的近似值:e=1+1/1!+1/2!+1/3!+1/4!+…+1/i!;当i=10000 20000…100000;
思路 : 先将第一项取出,从第二项开始使用for循环;int i=1;最后再将1加到结果上;
public class test4_11 {
public static void main(String[] args){
double e=0;
double sum=0;
for(int i=1;i<=10000;i++){
double multi=1;
for(int j=1;j<=i;j++){
multi=multij; //阶乘的算法:将所有j可能取到的值进行累乘;
}
sum+=1/multi;
e=1+sum;
}
System.out.println(e);
}
}
例题4计算pi:PI=4(1- 1/3+1/5- 1/7+…+1/(2i-1))
解:public static void main(String[] args){
double PI=0;
double sum=0;
for(int i=1;i<1000;i++){
sum=sum+Math.pow(-1,i+1)/2
i-1;
PI=4sum;
}
System.out.println(PI);
}
例题5显示闰年,显示101到2100年期间的所有闰年,每行显示十个,数字之间用一个空格隔开,同时显示这期间闰年的数目;
代码如下:
public static void main(String[] args){
int count=0;
for(int year=101;year<=2100;year++){
if((year%40&&year%100!=0 ) || (year%4000){
System.out.print(year+" ");
count++;
if(count%100){ ///每行显示十个后换行
System.out.println();
}
}
}
System.out.print("\n"+count); ////显示完年份后换行 再显示数目
}
例题6
编写程序,当用户输入年份和该年第一天是星期几代表的数字,就显示该年每个月第一天是星期几;
public class test4_13 {
public static void main(String[] args) {
int year;
int week;
Scanner scanner=new Scanner(System.in);
System.out.println(“enter the year and week”);
year=scanner.nextInt();
week=scanner.nextInt();
int m=0,y=0,k=0,j=0,h=0;
String monthStr="";
String weekStr="";
for(int month=1;month<=12;month++){
m=month;
y=year;
if (m
1||m==2){
m=m+12;
y–;
}
k=y%100;
j=y%100;
h=(1+26
(month+1)/10+k+k/4+j/4+5*j)%7;
switch(month){
case 1:
monthStr=“January”;
break;
case 2:
monthStr=“February”;
break;
case 3:
monthStr=“March”;
break;
case 4:
monthStr=“April”;
break;
case 5:
monthStr=“May”;
break;
case 6:
monthStr=“June”;
break;
case 7:
monthStr=“July”;
break;
case 8:
monthStr=“August”;
break;
case 9:
monthStr=“September”;
break;
case 10:
monthStr=“October”;
break;
case 11:
monthStr=“November”;
break;
case 12:
monthStr=“December”;
break;
}
switch(week){
case 0:
weekStr=“Saturday”;
break;
case 1:
weekStr=“Sunday”;
break;
case 2:
weekStr=“Monday”;
break;
case 3:
weekStr=“Tuesday”;
break;
case 4:
weekStr=“Wednesday”;
break;
case 5:
weekStr=“Thursday”;
break;
case 6:
weekStr=“Friday”;
break;
}
System.out.println(monthStr+“1,”+year+“is”+weekStr);
}
}
}
**例题7:**石头剪刀布游戏,当用户或者电脑赢对手两次以上则游戏结束:
public static void main(String[] args) {
int cCount=0; ///代表电脑赢的次数
int pCount=0;///代表玩家赢得次数
Scanner scanner=new Scanner(System.in);
while(true){
System.out.println(“请输入0剪刀,1石头,2布”);
int p=scanner.nextInt();
int c=(int)(Math.random()*3);//产生1到3随机数
if(pc){
System.out.println(“平局,请继续!”);
}
if(p
0&&c1){
System.out.println(“玩家剪刀,电脑石头,电脑赢!”);//////电脑和玩家出拳的六种情况:
cCount++;
}
if(p
0&&c2){
System.out.println(“玩家剪刀,电脑布,玩家赢!”);
pCount++;
}
if(p
1&&c0){
System.out.println(“玩家石头,电脑剪刀,玩家赢!”);
pCount++;
}
if(p
1&&c2){
System.out.println(“玩家石头,电脑布,电脑赢!”);
cCount++;
}
if(p
2&&c0){
System.out.println(“玩家布,电脑剪刀,电脑赢!”);
cCount++;
}
if(p
2&&c1){
System.out.println(“玩家布,电脑石头,玩家赢!”);
pCount++;
}
if(cCount
2||pCount2){
break;
}
}
if(cCount
2){
System.out.println(“最终电脑赢!”);
}else{
System.out.println(“最终玩家赢!”);
}
}
**例题8:**编写程序,显示1到7之间所有两个数字的组合:
public static void main(String[] args){
int count=0;
for(int i=1;i<=6;i++){ ////组合的第一个数字
for(int j=i+1;j<=7;j++){ ///组合的第二个数字
System.out.print(i+" "+j);
System.out.println();
count++;
}
}
System.out.println(“count is:”+count);
}

猜你喜欢

转载自blog.csdn.net/zhangpupu320/article/details/83020096