Java第五次作业
1.
分别使用for循环,while循环,do循环求1到100之间所有能被3整除的整数的和。(知识点:循环语句)
package as; public class jt1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int i; int sum=0; for(i=0;i<=100;i++){ if(i%3==0){ sum=sum+i; } } System.out.println(sum); } }
package as; public class jt2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int i=0; int sum=0; while(i<=100){ if(i%3==0){ sum=sum+i; } i++; } System.out.println(sum); }
package as; public class jt3 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int i=0; int sum=0; do{ i++; if(i%3==0){ sum=sum+i; } }while(i<=100); System.out.println(sum); } }
2.
输出0-9之间的数,但是不包括5。
package as; public class j2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int i=0; while(i<=9){ if(i!=5){ System.out.println(i); } i++; } } }
3.
编写一个程序,求整数n的阶乘
package as; import java.util.Scanner; public class j3 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("输入数"); Scanner sc=new Scanner(System.in); int i=sc.nextInt(); int sum=1; for(int a=1;a<=i;a++){ sum=sum*a; } System.out.println("阶乘"+sum); } }
4.
编写一个程序,输入任意学生成绩,如果输入不合法(<0或者>100),提示输入错误,重新输入,直到输入合法程序结束
package as; import java.util.Scanner; public class j4 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("请输入成绩"); Scanner sc=new Scanner(System.in); int a=sc.nextInt(); if(a<0||a>100){ System.out.println("输入有问题,请重新输入"); a=sc.nextInt(); } System.out.println("成绩为"+a); } }
5.
假设某员工今年的年薪是30000元,年薪的年增长率6%。编写一个Java应用程序计算该员工10年后的年薪,并统计未来10年(从今年算起)总收入。
package ss; public class jt5 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int i=0; double n=30000; double sum=n; for(i=0;i<=9;i++){ n=n*1.06; sum=sum+n; } System.out.println("十年后工资"+n); System.out.println("十年工资总和"+sum); } }
6.打印水仙花数
package bbc; import java.util.Scanner; public class j5 { public static void main(String[] args) { System.out.println("请输入一三位个数"); Scanner scanner=new Scanner(System.in); int a=scanner.nextInt(); int ge,shi,bai; ge=a%10; shi=a%100/10; bai=a/100; if(ge*ge*ge+shi*shi*shi+bai*bai*bai==a) { System.out.println("这个数是水仙花数"); } else { System.out.println("这个数不是水仙花数"); } } }
7.输入年月日,输出是该年第几天
package eee; import java.util.Scanner; public class Test2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int[] Year = { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }; int[] Year = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }; Scanner input = new Scanner(System.in); System.out.print("请输入年份:"); int year = input.nextInt(); System.out.print("请输入月份:"); int month = input.nextInt(); System.out.print("请输入天数:"); int day = input.nextInt(); int sum = 0; if (month > 12 || month < 1) { System.out.println("请输入正确的月份!"); } else { if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { if (day < 1 || day > 31) { System.out.println("请输入正确的天数!"); } else { sum = Year[month - 1] + day; } } else if (month == 2) { if (day < 1 || day > 29) { System.out.println("请输入正确的天数!"); } else { sum = Year[month - 1] + day; } } else { if (day < 1 || day > 30) { System.out.println("请输入正确的天数!"); } else { sum = Year[month - 1] + day; } } } else { if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { if (day < 1 || day > 31) { System.out.println("请输入正确的天数!"); } else { sum = cYear[month - 1] + day; } } else if (month == 2) { if (day < 1 || day > 28) { System.out.println("请输入正确的天数!"); } else { sum = cYear[month - 1] + day; } } else { if (day < 1 || day > 30) { System.out.println("请输入正确的天数!"); } else { sum = cYear[month - 1] + day; } } } } System.out.println("是这一天为一年中的第" + sum + "天"); } }
8.输入一四位整数,反向输出
package bbc; import java.util.Scanner; public class j6 { public static void main(String[] args) { System.out.println("请输入一四位数"); Scanner sc=new Scanner(System.in); int i; int a,b,c,d,s; i=sc.nextInt(); a=i/1000; b=i%1000/100; c=i%100/10; d=i%10; s=d*1000+c*100+b*10+a; System.out.println("逆序为"+s); } }