一,反射
package test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class FanShe {
public void sayHello(String name){
System.out.println("hello," + name);
}
public void say(){
System.out.println("你好啊");
}
public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException
{
Object f = new FanShe();
// 用反射的方式调用o的sayHello方法,不得使用强制类型转换实现
Class class1 = f.getClass();
Method method = class1.getDeclaredMethod("sayHello",String.class);
method.invoke(f,"你好");
Method method2 = class1.getDeclaredMethod("say");
method2.invoke(f);
}
}
二,找最大和最小
package test;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class FindMax{
public static ArrayList<Integer> px(ArrayList<Integer> l){
ArrayList<Integer> al=new ArrayList<Integer> ();
int size = l.size();
for(int i=0;i<size;i++){
int temp=l.get(0);
int p=0;
for(int j=0;j<l.size();j++){
if(l.get(j)>temp){
temp=l.get(j);
p=j;
}
}
al.add(temp);
l.remove(p);
}
return al;
}
public static void main(String[] agrs) {
ArrayList <Integer> al=new ArrayList<Integer>();
for(int i=0;i<10;i++){
Random r = new Random();
int tmp = r.nextInt(50) + 50;
al.add(tmp);
}
System.out.println("排序前的随机生成数"+al);
System.out.println("排序后的随机生成数"+px(al));
}
}
三,找字母在文章中出现的次数
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
class Word {
private static int num=0;//记录test数目
public void run() {
String str = "Youth is not a time of life; it is a state of mind; it is not a matter of rosy cheeks, " +
"red lips and supple knees; it is a matter of the will, a quality of the imagination, \r\na" +
" vigor of the emotions; it is the freshness of the deep springs of life.\r\n IS Is iS isp ";
String[] ss=str.split("\\W+");
for(int i=0;i<ss.length;i++){
System.out.println(ss[i]);
}
for(String e:ss)
if("is".equalsIgnoreCase(e))
num++;//如果单词是is则记录
System.out.println(""is" is: "+num);//打印is个数
}
}
public class FindNum {
public static void main(String[] args){
Word w=new Word();
w.run();
String str = "word:love property:v meaning:爱";
String[] strs = str.split("[ ]?\\w+:");//正则表达式
System.out.println(strs[1]);
System.out.println(strs[2]);
System.out.println(strs[3]);
String ss="as11jdflkj1aslkdf1jlk1as";
String[] split = ss.split("1");
for(int i=0;i<split.length;i++){
System.out.println(i+"-----"+split[i]);
}
}
}
四,税率计算
package test;
public class sdsmain {
static double d_sdsMin = 2000; // 所得税起征点
static double[] gade = { 0, 500, 2000, 5000, 20000, 40000, 60000, 80000,
100000,900000000 }; // 月收入
static double[] rate = { 5, 10, 15, 20, 25, 30, 35, 40, 45 }; // 税率
static double[] sds = { 0, 25,175, 625, 3625, 8625, 14625, 21625,29625, 900000000 }; // 月所得税
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(toSds(5136.6));
System.out.println(toPay(652.32));
}
public static double toSds(double d_pay) {
if (d_pay < d_sdsMin || d_pay < gade[1])
return 0; // 如果收入过低则不收所得税
double d_sds_temp = 0;
for (int j = 1; j < 9; j++) {
if (d_pay > gade[j])
d_sds_temp = (gade[j] - gade[j - 1]) * rate[j-1]*0.01 + d_sds_temp;
if (d_pay <= gade[j]) {
d_sds_temp = (d_pay - gade[j-1]) * rate[j-1]*0.01 + d_sds_temp;
break;
}
}
return d_sds_temp;
}
public static double toPay(double d_sds) {
double d_pay_temp = 0;
if (d_sds<=0)
return 0; //
for (int j = 9; j >0; j--) {
if(d_sds>sds[j]) {
d_pay_temp=gade[j]+(d_sds-sds[j])/rate[j]*100;
break;
}
}
return d_pay_temp;
}
}
五,输入输出流
package test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class ShuRuOrShuChu {
public static void createFile(File f)
{// 向文件f中随机写入三行字符串
FileOutputStream fos = null;
BufferedWriter bw = null;
try {
fos = new FileOutputStream(f);
bw = new BufferedWriter(new OutputStreamWriter(fos));
StringBuffer a;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for(int i=0;i<3;i++){
System.out.println("请输入br的值");
String aString = br.readLine();
System.out.println("br的值是:"+aString);
bw.write(aString);
bw.write("\r\n");
}
}
catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
finally {
try {
if (bw != null)
bw.close();
if (fos != null)
fos.close();
}
catch (IOException ie) {
}
}
}
public static void readFile(File f)
{// 读出文件f的内容,并打印出
BufferedReader reader = null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(f));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
System.out.println("line " + line + ": " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
File file = new File("D:\\abc.txt");
createFile(file);
readFile(file);
}
}
六,显示/隐藏第二列
Hidden.html如下
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> 显示隐藏列 </TITLE>
<SCRIPT LANGUAGE="JavaScript">
function hidden(table,num){
for(i=0;i<table.rows.length;i++){
table.rows[i].cells[num].style.display=table.rows[i].cells[num].style.display=="none"?"block":"none";
}
}
</SCRIPT>
</HEAD>
<BODY>
<TABLE id="Table1" cellSpacing="1" cellPadding="1" border="1">
<tr><td>1</td><td>2</td><td>3</td></tr>
<tr><td>4</td><td bgcolor="red">5</td><td>6</td></tr>
<tr><td>7</td><td bgcolor="green">8</td><td>9</td></tr>
<tr><td>10</td><td bgcolor="yellow">11</td><td>12</td></tr>
</TABLE>
<input type="button" value="隐藏第二列" onclick="hidden(document.getElementByIdx_x('Table1'),1)"/>
</BODY>
</HTML>
七,全选-----显示选择的选项
t.html如下
<html>
<head>
<script>
function selectAll(){
//返回一个数组
var arr = ['c1','c2','c3'];
for(i=0;i<arr.length;i++){
alert(arr[i]);
document.getElementByIdx_x(arr[i]).checked = document.getElementByIdx_x(arr[i]).checked == true?false:true;
}
}
function selectAll2(){
var interest=document.getElementsByName("interest");
for( i=0;i<interest.length;i++){
if(interest[i].type=="checkbox"){
interest[i].checked=interest[i].checked==true?false:true;
}
}
}
</script>
</head>
<body style="font-size:30pt;" background="red">
<form action="test.jsp" method="post" >
name:<input type="text" name="name" id="name"/>
<input type="button" value="testProtoType" onclick="testProtoType();"/><br/>
sleep:<input type="checkbox" name="interest" id="c1" value="sleep"/>
play:<input type="checkbox" name="interest" id="c2" value="play"/>
eating:<input type="checkbox" name="interest" id="c3" value="eating"/>
<input type="button" value="selectAll" onclick="selectAll2();"/>
<!-- <a href="tt.jsp">toShow</a>-->
<input type="submit" value="提交"/>
</form>
</body>
</html>
test.jsp如下
<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<c:forEach var="inte" items="${paramValues.interest}">
${inte}
</c:forEach>
您选中的人员名单为:
<% String [] a=request.getParameterValues("interest");
for(int i=0;i<a.length;i++){
out.println(a[i]);
}
%>
-------------${paramValues.interest}
</body>
</html>