第八章练习

package t8;
import java.util.*;
public class Test8 {
	public static void main(String args[]){
		String a=new String("1321");
		System.out.println(a);
		
		
		String str=new String("abc");    //字符大小写转换及提取对应位置字符,大小写转换无论是做方法还是在main里都必须赋值(直接输出则不必)
		System.out.println(str);
		str=str.toUpperCase();
		System.out.println(str);
		str=str.toLowerCase();
		System.out.println(str);
		String str2=new String("123def");
		str=str.concat(str2);
		System.out.println(str);
		System.out.println(str.charAt(0));
		System.out.println(str.charAt(str.length()-1));
		
		
		
		Calendar calendar=Calendar.getInstance();        //日期间的相隔天数,1 转换为毫秒 2 将日期相减 3 除以公制的时间单位得到天数
		calendar.setTime(new Date());
		int year=calendar.get(Calendar.YEAR),
				month=calendar.get(Calendar.MONTH),
				day=calendar.get(Calendar.DAY_OF_MONTH),
				hour=calendar.get(Calendar.HOUR_OF_DAY),
				minute=calendar.get(Calendar.MINUTE),
				second=calendar.get(Calendar.SECOND);
		System.out.println("现在的时间是:");
		System.out.println(""+year+"年"+month+"月"+day+"日");
		System.out.println(" "+hour+"时"+minute+"分"+second+"秒");
		int y=2012,m=9,d=1;
		calendar.set(y,m-1,d);
		long time1=calendar.getTimeInMillis();
		y=2016;
		m=7;
		day=1;
		calendar.set(y,m-1,d);
		long time2=calendar.getTimeInMillis();
		long subDay=(time2-time1)/(1000*60*60*24);
		System.out.println(""+new Date(time2));
		System.out.println("与"+new Date(time1));
		System.out.println("相隔"+subDay+"天");
		
		double f=-457.12;
		double g=Math.abs(f);	//绝对值
		System.out.println(g);
		double h=Math.max(f, g);	//最大值
		System.out.println(h);
		h=Math.min(f, g);		//最小值
		System.out.println(h);	
		System.out.println(Math.random()); //随机数(直接打印)
		Random r=new Random();      //随机生成1-10区间的随机数(每次执行都重新获取一个)
		int i=Math.abs(r.nextInt() % 10);
		System.out.println(i);
		
		int j=23,k=4;
		System.out.println(Math.pow(j, k));   
		double l=Math.pow(j, k);
		double m1=Math.sqrt(l);
		System.out.println(m1);
		
		System.out.println(Math.log(m1));  
		System.out.println(Math.sin(15));
		System.out.println(Math.asin(0.58));
		
		String str21="klsdjflk21314as21";	//替换字符
		String str22=str21.replaceAll("[a-zA-Z]+", "");
		System.out.println(str22);
		
		String grade=("数学87分,物理76分,英语96分");    //scanner 提取数据
		Scanner scanner=new Scanner(grade);
		scanner.useDelimiter("[^0123456789.]+");
		double sum=0;
		double avg=0;
		int g2=0;
		while(scanner.hasNext()) {
			try {
				double g1=scanner.nextDouble();
					sum=sum+g1;
					g2++;
					avg=sum/g2;
					System.out.println(g1);
			}
			catch(InputMismatchException exp){
				String t=scanner.next();
					
			
			}
		
		}
			System.out.println("总成绩:"+sum);
			System.out.println("平均成绩:"+avg);
				
		
	}

	
}

展开阅读全文

没有更多推荐了,返回首页