1. 打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。(知识点:循环语句、条件语句)
public class zxcvbn {
public static void main(String[] args) {
for(int i=100;i<1000;i++){
if(isOrNo(i)){
System.out.println(i+" ");
}
}
}
public static boolean isOrNo(int i){
int one;
int two;
int three;
one=i/100;
two=i%100/10;
three=i%10;
if(one*one*one+two*two*two+three*three*three!=i){
return false;
}
return true;
}
}
2.在控制台输出以下图形(知识点:循环语句、条件语句)
(1)
public class zxcvbn {
public static void main(String[] args) {
for(int i=1;i<7;i++)
{
for(int j=0;j<i;j++)
{
System.out.print(j+1);
}
System.out.println();
}
}
}
(2)
public class zxcvbn {
public static void main(String[] args) {
for(int i=1;i<7;i++) {
for(int j=1;j<=7-i;j++) {
System.out.print(j);
}
System.out.println();
}
}
}
(3)
public class zxcvbn {
public static void main(String[] args) {
int i,j;
for (i = 1; i <= 6; i++) {
for (j = 1; j <= 7 - i; j++) {
System.out.print(" ");
}
for (j = i; j >= 1; j--)
System.out.print(j);
System.out.println();
}
System.out.println();
}
}
3. 输入年月日,判断这是这一年中的第几天(知识点:循环语句、条件语句)
import java.util.*;
public class zxcvbn {
public static void main(String[] args) {
int day=0;
int month=0;
int year=0;
int sum=0;
int leap;
System.out.print("请输入年,月,日\n");
Scanner input = new Scanner(System.in);
year=input.nextInt();
month=input.nextInt();
day=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=304;break;
case 12:
sum=334;break;
default:
System.out.println("data error");break;
}
sum=sum+day; /*再加上某天的天数*/
if(year%400==0||(year%4==0&&year%100!=0))/*判断是不是闰年*/
leap=1;
else
leap=0;
if(leap==1 && month>2)/*如果是闰年且月份大于2,总天数应该加一天*/
sum++;
System.out.println("It is the the day:"+sum);
}
}
4.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句)
import java.util.Scanner;
public class zxcvbn {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int shuru = sc.nextInt();
String hebing ="";
String fanzhuan = String.valueOf(shuru);
for(int i=fanzhuan.length()-1; i>=0; i--) {
hebing = hebing + fanzhuan.charAt(i);//将数字拆开,重新组合形成一个字符串
}
int newShu = Integer.parseInt(hebing);//将新数转回int类型
System.out.print(newShu);
}
}
原文:https://www.cnblogs.com/fanbudufan/p/12618152.html