今天用eclipse写了3个简单的java程序,在缺省包中写的,代码如下:
import java.util.Scanner;
/*java程序题
*
任务一:输入某年某月某日,判断这一天是这一年的第几天。例如,2001 年 3 月 5 日
是这一年的第 64 天。
任务二:输出阶梯形式的 9*9 口诀表
任务三:编程实现判断一个整数是否为“水仙花数”。所谓“水仙花数”是指一个三位的整
数,
其各位数字立方和等于该数本身。
例如:
153 是一个“水仙花数”,
因为 153=13+53+33。
*/
public class Test {
public static void main(String[] args){
//第一题
System.out.println("你给的日期:");
/*
* 接收控制台输入的内容,
* 要求的格式为 xxxx年x月x日
* 输入的年月日必须符合实际要求,比如年必须大于0,月必须在1和12月之间,日必须在1和31之间
* 如没按要求输入,将有错误报出
*/
Scanner strInput = new Scanner(System.in);
String strDate = strInput.next();
System.out.println("你给的日期是那一年中的第 " + getDay(strDate) + "天");
System.out.println();
//第二题
System.out.println("九九乘法表:");
nine();
System.out.println();
//第三题
System.out.println("你给的数:");
/*接收控制台输入的内容
* 内容必须为数字,不能为其他的字符
* 如没按要求输入,将有错误报出
*/
Scanner input = new Scanner(System.in);
int number = input.nextInt();
System.out.println(testNumber(number));
}
/*
*第一题
*/
public static int getDay(String strDate){
/*
*从给定的字符串中分别获取年,月,日 并分别转换成int类型
*/
int year = Integer.valueOf(strDate.substring(0,strDate.indexOf("年")));
int month = Integer.valueOf(strDate.substring(strDate.indexOf("年")+1,strDate.indexOf("月")));
int day = Integer.valueOf(strDate.substring(strDate.indexOf("月")+1,strDate.indexOf("日")));
int sum = 0; //所在天数
int count = 0; //月份增量
while(month > count){ //从第一月份开始循环,循环到给定的月份的前一月为止
switch(count){
/*
* 1,3,5,7,8,10,12月是31天
*/
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
sum += 31;
break;
/*
* 4,6,9,11是30天
*/
case 4:
case 6:
case 9:
case 11:
sum += 30;
break;
/*
* 闰年2月是29天,平年2月是28天
*/
case 2:
if(year % 4 == 0 && year % 400 == 0)
sum += 29;
else
sum += 28;
break;
}
count ++;
}
return sum + day;
}
/*
* 第二题
*/
public static void nine(){
for(int i=1; i<10; i++){
for(int j=1; j<i+1; j++){
System.out.print(i + " * " + j + " = " + i * j + "\t");
}
System.out.println();
}
}
/*
* 第三题
*/
public static boolean testNumber(int num){
String strNumber = num+""; //把给定的int类型的数转换成字符串类型
int sum = 0;
for(int i=0; i<strNumber.length(); i++){
sum += Math.abs(Integer.valueOf(strNumber.charAt(i))); //分割字符串中的数值字符,然后求出每个数的平方
}
return sum == num;
}
}
有其他方法,请贴上!!!