第三次上机练习
Posted y611lx
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第三次上机练习相关的知识,希望对你有一定的参考价值。
1.打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。(知识点:循环语句、条件语句)
package a; import java.util.Scanner; public class sss { public static void main(String[] args) { // TODO Auto-generated method stub for (int i = 100; i < 1000; i++) { int a = i / 100; int b = i / 10 % 10; int c = i % 10; if (Math.pow(a, 3) + Math.pow(b, 3) + Math.pow(c, 3) == i) System.out.println("水仙花数 : " +i); } } }
2.在控制台输出以下图形(知识点:循环语句、条件语句)( 四个位置经过翻转的直角三角形)
package a; import java.util.Scanner; public class sss { public static void main(String[] args) { // TODO Auto-generated method stub for(int a = 1;a <= 6;a++) { for(int i = 1;i <= a;i++) System.out.printf("%d ",i); System.out.print(" "); } System.out.print(" "); for(int a = 6;a >= 1;a--) { for(int i = 1;i <= a;i++) System.out.printf("%d ",i); System.out.print(" "); } System.out.print(" "); for(int a = 1;a <= 6;a++) { for(int i = 1;i <= 2 * (6 - a);i++) System.out.print(" "); for(int i = a;i >= 1;i--) System.out.printf("%d ",i); System.out.print(" "); } System.out.print(" "); for(int a = 6;a >= 1;a--) { for(int i = 1;i <= 2 *(6-a);i++) System.out.print(" "); for(int i = 1;i <= a;i++) System.out.printf("%d ",i); System.out.print(" "); } } }
3.输入年月日,判断这是这一年中的第几天(知识点:循环语句、条件语句)
package a; import java.util.Scanner; public class sss { public static void main(String[] args) { // TODO Auto-generated method stub int year, month, day; int days = 0; int d = 0; int e; input fymd = new input(); do { e = 0; System.out.print("输入年"); year =fymd.input(); System.out.print("输入月"); month = fymd.input(); System.out.print("输入天"); day = fymd.input(); if (year < 0 || month < 0 || month > 12 || day < 0 || day > 31) { System.out.println("输入错误请重新输入"); e=1 ; } }while( e==1); for (int i=1; i <month; i++) { switch (i) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: days = 31; break; case 4: case 6: case 9: case 11: days = 30; break; case 2: if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) { days = 29; } else { days = 28; } break; } d += days; } System.out.println(year + "-" + month + "-" + day + "是这年的第" + (d+day) + "天。"); } } class input{ public int input() { int value = 0; Scanner s = new Scanner(System.in); value = s.nextInt(); return value; } }
4.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句)
package a; import java.util.Scanner; public class sss { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); System.out.println("请输入一个四位数"); int n=sc.nextInt(); int a = n / 1000; int b = n / 100 % 10; int c = n / 10 % 10; int d = n % 10; int s = d * 1000 + c * 100 + b * 10 + a; System.out.println("反转后数为" + s); } }
以上是关于第三次上机练习的主要内容,如果未能解决你的问题,请参考以下文章