1.写一个方法,用一个for循环打印九九乘法表


/** *//**
* 打印九九乘法口诀表
*/

public void nineNineMulitTable()
{

for (int i = 1,j = 1; j <= 9; i++)
{
System.out.print(i+"*"+j+"="+i*j+" ");

if(i==j)
{
i=0;
j++;
System.out.println();
}
}
}

2.给定一个java.util.Date对象,如何转化为”2007-3-22 20:23:22”格式的字符串

/** *//**
* 将某个日期以固定格式转化成字符串
* @param date
* @return str
*/
public String date2FormatStr(Date date)

{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = sdf.format(date);
return str;
}
3.写一个方法,能够判断任意一个整数是否素数

/** *//**
* 判断任意一个整数是否素数
* @param num
* @return boolean
*/
public boolean isPrimeNumber(int num)

{

for (int i = 2; i <= Math.sqrt(num); i++)
{
if(num%i==0)

{
return false;
}
}
return true;
}
4.写一个方法,输入任意一个整数,返回它的阶乘

/** *//**
*获得任意一个整数的阶乘
*@param n
*@returnn!
*/
public int factorial(int num)

{
//递归
if(num == 1)

{
return 1;
}
return num*factorial(num-1);
}

5.写一个方法,用二分查找法判断任意整数在任意整数数组里面是否存在,若存在就返回它在数组中的索引位置,不存在返回-1

/** *//**
*二分查找特定整数在整型数组中的位置(递归)
*@param dataset
*@param data
*@param beginIndex
*@param endIndex
*@return index
*/

public int binarySearch(int[] dataset,int data,int beginIndex,int endIndex)
{
int midIndex = (beginIndex+endIndex)/2;
//如果查找的数要比开始索引的数据要小或者是比结束索引的书要大,或者开始查找的索引值大于结束的索引值返回-1没有查到

if(data <dataset[beginIndex]||data>dataset[endIndex]||beginIndex>endIndex)
{
return -1;
}

if(data <dataset[midIndex])
{
return binarySearch(dataset,data,beginIndex,midIndex-1);
}else if(data>dataset[midIndex])

{
return binarySearch(dataset,data,midIndex+1,endIndex);

}else
{
return midIndex;
}
}

/** *//**
*二分查找特定整数在整型数组中的位置(非递归)
*@param dataset
*@param data
*@return index
*/
public int binarySearch(int[] dataset ,int data)

{
int beginIndex = 0;
int endIndex = dataset.length - 1;
int midIndex = -1;

if(data <dataset[beginIndex]||data>dataset[endIndex]||beginIndex>endIndex)
{
return -1;
}

while(beginIndex <= endIndex)
{
midIndex = (beginIndex+endIndex)/2;

if(data <dataset[midIndex])
{
endIndex = midIndex-1;

} else if(data>dataset[midIndex])
{
beginIndex = midIndex+1;

}else
{
return midIndex;
}
}
return -1;
}
减肥瘦身品总汇
值得信赖*脉脉美妆*正品现货谢绝讲价
〓深港商盟〓名品欧衣坊(美国休闲品牌AF系列)
QQ三国游戏币及道具专卖店
小脸红红的瘦身旗舰店
posted on 2009-05-01 12:47
龙华城 阅读(4116)
评论(9) 编辑 收藏