{
String s1,s2,s3,t1=\s1=t1.toUpperCase(); s2=t1.toLowerCase(); s3=s1.concat(s2); g.drawString(s1,30,60); g.drawString(s2,30,90); g.drawString(s3,30,120); } }
4、 String 类的 public char charAt(int index) 方法可以得到当前字符串 index 位置上的一个字符。编写程序使用该方法得到一个字符串中的第一个和最后一个字符。 class E
{ public static void main(String args[]) { String s=\中国科学技术大学 \
char a=s.charAt(0),b=s.charAt(s.length()-1); System.out.print(a); System.out.println(b); } }
5、输出某年某月的日历页,通过 main 方法的参数将年份和月份时间传递到程序中。 import java.util.*; class Ex
{ public static void main(String args[]) {
int year,month; try {
year=Integer.parseInt(args[0]); month=Integer.parseInt(args[1])+1; }
catch(NumberFormatException e) {
year=2004; month=1; }
System.out.println(\日 一 二 三 四 五 六 \Calendar 日历 =Calendar.getInstance(); 日历 .set(year,month,1);
int 星期几 = 日历 .get(Calendar.DAY_OF_WEEK)-1; String a[]=new String[ 星期几 +31]; for(int i=0;i< 星期几 ;i++) { a[i]=\
17
}
for(int i= 星期几 ,n=1;i< 星期几 +31;i++) { if(n<=9)
a[i]=String.valueOf(n)+\else
a[i]=String.valueOf(n) ; n++; }
for(int i=0;i { System.out.println(\} System.out.print(\} } } 6、计算某年、某月、某日和某年、某月、某日之间的天数间隔。要求年、月、日通过 main 方法的参数传递到程序中。 class Ex { public static void main(String args[]) { int year1,month1,day1,year2,month2,day2; try { year1=Integer.parseInt(args[0]); month1=Integer.parseInt(args[1]); day1=Integer.parseInt(args[2]); year2=Integer.parseInt(args[3]); month2=Integer.parseInt(args[4]); day2=Integer.parseInt(args[5]); } catch(NumberFormatException e) { year1=2004; month1=0; day1=1; year2=2004; month2=0; day2=1; } Calendar calendar=Calendar.getInstance(); calendar.set(year1,month1,day1); long timeYear1=calendar.getTimeInMillis(); 18 calendar.set(year2,month2,day2); long timeYear2=calendar.getTimeInMillis(); long 相隔天数 =Math.abs((timeYear1-timeYear2)/(1000*60*60*24)); System.out.println(\年 \月 \日和 \year2+\年 \月 \日相隔 \相隔天数 +\天 \} } 7、编程练习 Math 类的常用方法。 class E { public static void main(String args[]) { double a=0,b=0,c=0; a=12; b=24; c=Math.max(a,b); System.out.println(c); c=Math.min(a,b); System.out.println(c); c=Math.pow(2,3); System.out.println(c); c=Math.abs(-0.123); System.out.println(c); c=Math.asin(0.56); System.out.println(c); c=Math.cos(3.14); System.out.println(c); c=Math.exp(1); System.out.println(c); c=Math.log(8); System.out.println(c); } } 19
相关推荐: