1.写一个方法,用一个for循环打印九九乘法表
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
/** */
/**
* 打印九九乘法口诀表
*/
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
public
void
nineNineMulitTable()
{
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (int i = 1,j = 1; j <= 9; i++)
{
System.out.print(i+"*"+j+"="+i*j+" ");
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
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)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (int i = 2; i <= Math.sqrt(num); i++)
{
if(num%i==0)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
}
return true;
}
4.写一个方法,输入任意一个整数,返回它的阶乘
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
/** */
/**
*获得任意一个整数的阶乘
*@param n
*@returnn!
*/
public
int
factorial(
int
num)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//递归
if(num == 1)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return 1;
}
return num*factorial(num-1);
}
5.写一个方法,用二分查找法判断任意整数在任意整数数组里面是否存在,若存在就返回它在数组中的索引位置,不存在返回-1
/** */
/**
*二分查找特定整数在整型数组中的位置(递归)
*@param dataset
*@param data
*@param beginIndex
*@param endIndex
*@return index
*/
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
public
int
binarySearch(
int
[] dataset,
int
data,
int
beginIndex,
int
endIndex)
{
int midIndex = (beginIndex+endIndex)/2;
//如果查找的数要比开始索引的数据要小或者是比结束索引的书要大,或者开始查找的索引值大于结束的索引值返回-1没有查到
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(data <dataset[beginIndex]||data>dataset[endIndex]||beginIndex>endIndex)
{
return -1;
}
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(data <dataset[midIndex])
{
return binarySearch(dataset,data,beginIndex,midIndex-1);
}else if(data>dataset[midIndex])
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return binarySearch(dataset,data,midIndex+1,endIndex);
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}else
{
return midIndex;
}
}
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
/** */
/**
*二分查找特定整数在整型数组中的位置(非递归)
*@param dataset
*@param data
*@return index
*/
public
int
binarySearch(
int
[] dataset ,
int
data)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
int beginIndex = 0;
int endIndex = dataset.length - 1;
int midIndex = -1;
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(data <dataset[beginIndex]||data>dataset[endIndex]||beginIndex>endIndex)
{
return -1;
}
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
while(beginIndex <= endIndex)
{
midIndex = (beginIndex+endIndex)/2;
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(data <dataset[midIndex])
{
endIndex = midIndex-1;
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} else if(data>dataset[midIndex])
{
beginIndex = midIndex+1;
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}else
{
return midIndex;
}
}
return -1;
}