第三次上机作业
1. 打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。(知识点:循环语句、条件语句)
public class One {
public static void main(String[] args) {
int s=0;
for (int a = 1;a<10;a++) {
for (int b = 0; b < 10; b++) {
for (int c = 0; c < 10; c++) {
s=100*a+10*b+c;
if (s==(a*a*a+b*b*b+c*c*c)) {
System.out.print(s+" ");
}
}
}
}
}
}

2.在控制台输出以下三角·(知识点:循环语句、条件语句)
public class One {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
for (int i = 1; i <=6 ; i++) {
for (int j = 1; j <=i; j++) {
System.out.print(j);
}
System.out.println();
}
}
}
public class One {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
for (int i = 1; i <=6 ; i++) {
for (int j = 1; j<=6-i; j++) {
System.out.print(" ");
}
for (int j = 1; j <=i; j++) {
System.out.print(j);
}
System.out.println();
}
}
}
public class One {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
for (int i = 0; i <=6 ; i++) {
for (int j = 1; j<=i; j++) {
System.out.print(" ");
}
for (int j = 1; j <=6-i; j++) {
System.out.print(j);
}
System.out.println();
}
}
}
public class One {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
for (int i = 1; i <=6 ; i++) {
for (int j = i; j <=6; j++) {
System.out.print(j);
}
System.out.println();
}
}
}

3. 输入年月日,判断这是这一年中的第几天(知识点:循环语句、条件语句)
import java.util.*;
public class One {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("请输入年份: ");
int y=input.nextInt();
System.out.print("请输入月份: ");
int m=input.nextInt();
System.out.print("请输入日份: ");
int d=input.nextInt();
int sum=0;
switch (m) {
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=304;break;
case 12:sum=334;break;
default:System.out.println("error");break;
}
sum+=d;
if (y%400==0||(y%4==0&&y%100!=0)) {
if (m>2) {
System.out.println(y+"."+m+"."+d+"为第"+(sum+1)+"天");
} else {
System.out.println(y+"."+m+"."+d+"为第"+sum+"天");
}
} else {
System.out.println(y+"."+m+"."+d+"为第"+sum+"天");
}
}
}


4.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句)
import java.util.*; public class One { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.print("四位数: "); int a=input.nextInt(); int b,c,d,e; b=a%10; c=a%100/10; d=a%1000/100; e=a/1000; System.out.println(""+b+c+d+e); } }
package com.run.test; import java.util.*; public class One { public static void main(String []args) { Scanner input=new Scanner(System.in); System.out.print("输入四位数:"); long b=0; long sum=0; int a=input.nextInt(); while (a>0) { b=a%10; a=a/10; sum=sum*10+b; } System.out.println("结 果 为:"+sum); } }


浙公网安备 33010602011771号