4.2第五周上机练习和作业
上机练习
1.分别使用for循环,while循环,do循环求1到100之间所有能被3整除的整数的和。(知识点:循环语句)
for
1 import java.util.Locale; 2 import java.util.Scanner; 3 public class g { 4 public static void main(String[] args) { 5 int a,sum=0; 6 for(a=1;a<100;a++){ 7 if(a%3==0){ 8 sum+=a; 9 } 10 } 11 System.out.println(sum); 12 13 } 14 }
while
1 import java.util.Locale; 2 import java.util.Scanner; 3 public class g { 4 public static void main(String[] args) { 5 int a=0; 6 int sum=0; 7 while(a<=100){ 8 if(a%3==0){ 9 sum+=a; 10 }a++; 11 }System.out.println(sum); 12 } 13 }
do while
1 import java.util.Locale; 2 import java.util.Scanner; 3 public class g { 4 public static void main(String[] args) { 5 int a=0; 6 int sum=0; 7 do{ 8 if(a%3==0){ 9 sum+=a; 10 }a++; 11 }while(a<=100); 12 System.out.println(sum); 13 14 } 15 }

2.输出0-9之间的数,但是不包括5。(知识点:条件、循环语句)
1 import java.util.Locale; 2 import java.util.Scanner; 3 public class g { 4 public static void main(String[] args) { 5 int a; 6 7 for(a=0;a<10;a++){ 8 if(a!=5){ 9 System.out.println(a); 10 } 11 } 12 } 13 }

3.编写一个程序,求整数n的阶乘,例如5的阶乘是1*2*3*4*5(知识点:循环语句)
1 import java.util.Locale; 2 import java.util.Scanner; 3 public class g { 4 public static void main(String[] args) { 5 // TODO Auto-generated method stub 6 Scanner input=new Scanner(System.in); 7 int i=input.nextInt(); 8 int sum=1; 9 int a=1; 10 while(a<=i){ 11 sum=sum*a; 12 a++; 13 } 14 System.out.println(sum); 15 16 } 17 }

4.编写一个程序,输入任意学生成绩,如果输入不合法(<0或者>100),提示输入错误,重新输入,直到输入合法程序结束(知识点:循环语句)
1 import java.util.Locale; 2 import java.util.Scanner; 3 public class g { 4 public static void main(String[] args) { 5 // TODO Auto-generated method stub 6 Scanner input=new Scanner(System.in); 7 int a=input.nextInt(); 8 while(a>100||a<0){ 9 System.out.println("输入错误,重新输入"); 10 a=input.nextInt(); 11 } 12 System.out.println("输入合法"); 13 } 14 }

5.假设某员工今年的年薪是30000元,年薪的年增长率6%。编写一个Java应用程序计算该员工10年后的年薪,并统计未来10年(从今年算起)总收入。(知识点:循环语句)
1 import java.util.Locale; 2 import java.util.Scanner; 3 public class g { 4 public static void main(String[] args) { 5 // TODO Auto-generated method stub 6 int a=30000; 7 double b=0.06; 8 int year; 9 int sum=0; 10 for(year=1;year<=10;year++){ 11 a=(int)(a+a*b); 12 sum=(int)(sum+a); 13 } 14 System.out.println("10年后的年薪"+a); 15 System.out.println("未来10年总收入"+sum); 16 } 17 }

作业
1.打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。(知识点:循环语句、条件语句)
1 import java.util.Locale; 2 import java.util.Scanner; 3 public class g { 4 public static void main(String[] args) { 5 // TODO Auto-generated method stub 6 int a,b,c; 7 int i; 8 for(i=100;i<1000;i++){ 9 a=i%10; 10 b=i/10%10; 11 c=i/100; 12 if(a*a*a+b*b*b+c*c*c==i){ 13 System.out.println(i); 14 } 15 } 16 } 17 }

2.输入年月日,判断这是这一年中的第几天(知识点:循环语句、条件语句)
1 import java.util.Locale; 2 import java.util.Scanner; 3 public class g { 4 public static void main(String[] args) { 5 Scanner input=new Scanner(System.in); 6 System.out.println("请输入年:"); 7 int year=input.nextInt(); 8 System.out.println("请输入月:"); 9 int month=input.nextInt(); 10 System.out.println("请输入日:"); 11 int day=input.nextInt(); 12 int dayth=0,leap=0; 13 if(month > 12 || month < 1) 14 { 15 System.out.println("year out of limit\n"); 16 } 17 if(day > 31 || day < 1) 18 { 19 System.out.println(" month out of limit\n"); 20 } 21 if(year%4 == 0 &&year%100 != 0 || year%400 ==0) 22 { 23 leap = 29; 24 } 25 else 26 { 27 leap = 28; 28 } 29 month = month - 1; 30 switch(month) 31 { 32 case 11 : dayth += 30; 33 case 10 : dayth += 31; 34 case 9 : dayth += 30; 35 case 8 : dayth += 31; 36 case 7 : dayth += 31; 37 case 6 : dayth += 30; 38 case 5 : dayth += 31; 39 case 4 : dayth += 30; 40 case 3 : dayth += 31; 41 case 2 : dayth += leap; 42 case 1 : dayth += 31; 43 } 44 dayth += day; 45 System.out.println(dayth); 46 } 47 }

3.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句)
1 import java.util.Locale; 2 import java.util.Scanner; 3 public class g { 4 public static void main(String[] args) { 5 // TODO Auto-generated method stub 6 Scanner input=new Scanner(System.in); 7 System.out.println("请输入一个四位数:"); 8 int i= input.nextInt(); 9 int ge,shi,bai,qian,sum; 10 for (;i<1000||i>=10000;){ 11 System.out.println("请重新输入:"); 12 i= input.nextInt(); 13 } 14 ge=i%10*1000; 15 shi=i%100/10*100; 16 bai=i%1000/100*10; 17 qian=i/1000; 18 sum=ge+shi+bai+qian; 19 System.out.println(sum); 20 } 21 }

浙公网安备 33010602011771号