JAVA第五周上机练习
1.打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。(知识点:循环语句、条件语句)
package zuoye; import java.util.Scanner; public class arc{ public static void main(String[] args) { int a=0; int b=0; int c=0; int i=100; for(i=100;i<999;i++){ a=i/100; b=i/10%10; c=i%10; if(i==(a*a*a+b*b*b+c*c*c)) System.out.println(i); } } }
2.第一个图形
package zuoye; import java.util.Scanner; public class arc{ public static void main(String[] args) { int i=1; int j=1; for(i=1;i<=6;i++){ for(j=1;j<=i;j++){ System.out.print(j); } System.out.println(); } } }
第二个图形
package zuoye; import java.util.Scanner; public class arc{ public static void main(String[] args) { int i=6; int j=1; for(i=6;i>=0;i--){ for(j=1;j<=i;j++){ System.out.print(j); } System.out.println(); } } }
第三个图形
package zuoye; import java.util.Scanner; public class arc{ public static void main(String[] args) { int i=1; for(i=1;i<=6;i++){ for(int j=i;j>=1;j--){ System.out.print(j); } System.out.println(); } } }
第四个图形
package zuoye; import java.util.Scanner; public class arc{ public static void main(String[] args) { int i=6; int j=1; for(i=6;i>=1;i--){ for(j=1;j<=i;j++){ System.out.print(j); } System.out.println(); } } }
3.输入年月日,判断这是这一年中的第几天(知识点:循环语句、条件语句)
package zuoye; import java.util.Scanner; public class arc{ public static void main(String[] args) { System.out.print("请输入年,月,日\n"); Scanner input = new Scanner(System.in); int leap; int sum =0; int year = input.nextInt(); int month = input.nextInt(); int date = input.nextInt(); switch(month) { case 1 : sum=0; break; case 2 : sum =31; break; case 3: sum=59; break; case 4: sum =90; break; case 5: sum = 120; break; case 6: sum = 151; break; case 7: sum = 181; break; case 8: sum = 212; break; case 9: sum = 243; break; case 10: sum = 273; break; case 11: sum = 303; break; case 12: sum = 334; break; default: System.err.println("date error"); break; } sum=sum+date; if(year%400==0||year%4==0&&year%100!=0) { leap = 1; }else{ leap =0; } if(leap==1&&month>2) { sum++; } System.out.println(sum); } }
4.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句)
package zuoye; import java.util.Scanner; public class arc{ public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("请输入一个4位整数:"); int x=input.nextInt(); if(x>999&&x<=9999){ int gewei=x%10/10; int shiwei=x%100/10; int baiwei=x%1000/100; int qianwei=x/1000; int sum=qianwei+baiwei*10+shiwei*100+gewei*1000; System.out.println(sum); } else{ System.out.println("error"); } } }