1、编写Java程序,使用for循环打印菱形。
public static void main(String[] args)
{
int height = 17;
// 上层
int lineCount = (height + 1) / 2;
for (int line = 1; line <= lineCount; line++)
{
// 打印空格
int blankCount = (lineCount - line) * 2;
for (int blank = 1; blank <= blankCount; blank++)
{
System.out.print(" ");
}
// 打印星符
int starCount = line * 2 - 1;
for (int star = 1; star <= starCount; star++)
{
System.out.print("* ");
}
// 换行
System.out.println("");
}
// 下层
lineCount = (height - 1) / 2;
for (int line = lineCount; line >= 1; line--)
{
// 打印空格
int blankCount = (lineCount - line + 1) * 2;
for (int blank = 1; blank <= blankCount; blank++)
{
System.out.print(" ");
}
// 打印星符
int starCount = line * 2 - 1;
for (int star = 1; star <= starCount; star++)
{
System.out.print("* ");
}
// 换行
System.out.println("");
}
}
2、使用正则表达式来判断字符串是否为合法的手机号码。
String mobileNo = "13588888888";
String regex = "^[1](([3][0-9])|([4][5,7,9])|([5][^4,6,9])|([6][6])|([7][3,5,6,7,8])|([8][0-9])|([9][8,9]))[0-9]{8}$";
boolean result = mobileNo.matches(regex);
System.out.printf("手机号码:%s,校验结果:%b;", mobileNo, result);
3、使用冒泡排序,将数组从小到大排序。
方式一:
int[] arr = { 15, 21, 12, 34, 25, 42, 13, 28 };
for (int i = 1; i < arr.length; i++)
{
for (int j = 0; j < arr.length - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
方式二:
int[] arr = { 15, 21, 12, 34, 25, 42, 13, 28 };
for (int i = 0; i < arr.length; i++)
{
for (int j = i + 1; j < arr.length; j++)
{
if (arr[i] > arr[j])
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}