java第五周上机练习
作者:互联网
1. 打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。(知识点:循环语句、条件语句)
package qin; import java.util.*; public class ling { public static void main (String[]args){ int a=100,g,s,b,n=1; for(;a<1000;a++){ g=a%10; s=a/10%10; b=a/100; if(a==g*g*g+s*s*s+b*b*b){ System.out.println("第"+n+"个水仙花数是"+a); n++; } } } }
2.
在控制台输出以下图形(知识点:循环语句、条件语句)
package qin; import java.util.*; public class ling { public static void main (String[]args){ for(int i=1;i<=7;i++){ for(int j=1;j<i;j++){ System.out.print(j); } System.out.println(); } } }
package qin; import java.util.*; public class ling { public static void main (String[]args){ for(int i=6;i>=0;i--){ for(int j=1;j<=i;j++){ System.out.print(j); } System.out.println(); } } }
package qin; import java.util.*; public class ling { public static void main (String[]args){ int i,j; for(i=0;i<6;i++){ for(j=5;j>i;j--){ System.out.print(" "); } for(j=i+1;j>0;j--){ System.out.print(j); } System.out.println(); } } }
package qin; import java.util.*; public class ling { public static void main (String[]args){ for(int i=6;i>0;i--) { for(int k=0;k<6-i;k++) { System.out.print(" "); } for(int j=1;j<=i;j++) { System.out.print(j); } System.out.println(""); } } }
3. 输入年月日,判断这是这一年中的第几天(知识点:循环语句、条件语句)
package qin; import java.util.*; public class ling { public static void main (String[]args){ Scanner input = new Scanner(System.in); System.out.println("year"); int year = input.nextInt(); System.out.println("month"); int month = input.nextInt(); System.out.println("day"); int day = input.nextInt(); int total = 0; for(int i = 1;i< month; i++) { switch (i) { case 4: case 6: case 9: case 11: total += 30; break; case 2: if(year % 4 ==0&& year % 100 != 0|| year % 400 ==0) total +=29; else total+=28; break; default: total +=31; break; } } total+=day; System.out.println("该天是第"+total+"天"); } }
4.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句)
package qin; import java.util.*; public class ling { public static void main (String[]args){ System.out.print("请输入数字: "); Scanner sc = new Scanner(System.in); int x = sc.nextInt(); if(x>999 && x<=9999){ int gewei = x%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"); } } }
标签:qin,java,上机,int,练习,System,public,out 来源: https://www.cnblogs.com/qyling/p/12618935.html