- 打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。(知识点:循环语句、条件语句)
1 package text; 2 3 public class ZuoYe { 4 public static void main(String[] args) { 5 // TODO Auto-generated method stub 6 for(int i=100;i<=999;i++){ 7 int g=i%10; 8 int s=i/10%10; 9 int b=i/100%10; 10 if(i==g*g*g+s*s*s+b*b*b){ 11 System.out.println(i+"是一个水仙花数"); 12 } 13 } 14 } 15 }
-
在控制台输出以下图形(知识点:循环语句、条件语句)
1 package text; 2 3 public class ZuoYe { 4 public static void main(String[] args) { 5 // TODO Auto-generated method stub 6 for(int i=1;i<=6;i++){ 7 for(int j=1;j<=i;j++){ 8 System.out.print(j); 9 } 10 System.out.println(); 11 } 12 } 13 }
1 package text; 2 3 public class Text3 { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 for(int i=1;i<=6;i++){ 8 for(int j=1;j<=7-i;j++){ 9 System.out.print(j); 10 } 11 System.out.println(); 12 } 13 } 14 }
1 package text; 2 3 public class ZuoYe { 4 public static void main(String[] args) { 5 // TODO Auto-generated method stub 6 for (int i = 1; i <= 6; i++) { 7 for (int j = 1; j < 7 - i; j++) { 8 System.out.print(" "); 9 } 10 for (int k = i; k > 0; k--) { 11 System.out.print(k); 12 } 13 System.out.println(); 14 } 15 } 16 }
1 package lit; 2 3 public class next1 { 4 5 public static void main(String[] args){ 6 // TODO Auto-generated method stub 7 for(int i=6;i>=1;i--){ 8 for(int j=1;j<=7-i;j++){ 9 System.out.print(" "); 10 } 11 for (int k = 1; k <= i; k++){ 12 System.out.print(k); 13 } 14 System.out.println(" "); 15 } 16 } 17 }
3.输入年月日,判断这是这一年中的第几天(知识点:循环语句、条件语句)
1 package lit; 2 import java.util.Scanner; 3 public class next1 { 4 5 public static void main(String[] args){ 6 // TODO Auto-generated method stub 7 Scanner input = new Scanner(System.in); 8 System.out.println("year"); 9 int year =input.nextInt(); 10 11 System.out.println("month"); 12 int month =input.nextInt(); 13 14 System.out.println("day"); 15 int day =input.nextInt(); 16 17 int total=0; 18 //统计输入月份之前一共多少天 19 for(int i=1;i<month;i++){ 20 switch(i){ 21 case 4: 22 case 6: 23 case 9: 24 case 11: 25 total+=30; 26 break; 27 case 2: 28 if(year%4==0&&year%100!=0||year%400==0) 29 total+=29; 30 else total+=28; 31 break; 32 default: 33 total+=31; 34 break; 35 } 36 } 37 total+=day; 38 System.out.println("该天是"+total+"天"); 39 } 40 }
4.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句)
1 package lit; 2 import java.util.Scanner; 3 public class next1 { 4 5 public static void main(String[] args){ 6 // TODO Auto-generated method stub 7 Scanner sc = new Scanner(System.in); 8 int x =sc.nextInt(); 9 if(x>=1000 && x<=9999){ 10 int ge = x%10; 11 int shi = x % 100 / 10; 12 int bai = x%1000/100; 13 int qian = x/1000; 14 int sum = qian + bai*10 +shi*100 +ge*1000; 15 System.out.println(sum); 16 } 17 else{ 18 System.out.println("错误"); 19 } 20 } 21 }