冒泡排序:
public static void main(String[] args) {
int arr[] ={12,20,30,45,99,66,33,22,66,22,100};
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length - 1; j++) {
if(arr[j] < arr[j+1]){
int l = arr[j];
arr[j] = arr[j+1];
arr[j+1] = l;
}
}
}
for (int i : arr) {
System.out.print(i+" ");
}
}
快速排序:
public class QuickSort {
/**
* 快速排序
*
* @param strDate
* @param left
* @param right
*/
public void quickSort(String[] strDate, int left, int right) {
String middle, tempDate;
int i, j;
i = left;
j = right;
middle = strDate[(i + j) / 2];
do {
while (strDate[i].compareTo(middle) < 0 && i < right)
i++; // 找出左边比中间值大的数
while (strDate[j].compareTo(middle) > 0 && j > left)
j--; // 找出右边比中间值小的数
if (i <= j) { // 将左边大的数和右边小的数进行替换
tempDate = strDate[i];
strDate[i] = strDate[j];
strDate[j] = tempDate;
i++;
j--;
}
} while (i <= j); // 当两者交错时停止
if (i < right) {
quickSort(strDate, i, right);// 从
}
if (j > left) {
quickSort(strDate, left, j);
}
}
/**
* * @param args
*/
public static void main(String[] args) {
String[] strVoid = new String[] { "11", "66", "22", "0", "55", "22", "0", "32" };
QuickSort sort = new QuickSort();
sort.quickSort(strVoid, 0, strVoid.length - 1);
for (int i = 0; i < strVoid.length; i++) {
System.out.println(strVoid[i] + " ");
}
}
}
九九乘法表:
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j+"*"+i+"="+i*j+" ");
}
System.out.println(" ");
}
}
菱形:
public static void main(String[] args) {
int arr[] ={12,20,30,45,99,66,33,22,66,22,100};
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length - 1; j++) {
if(arr[j] < arr[j+1]){
int l = arr[j];
arr[j] = arr[j+1];
arr[j+1] = l;
}
}
}
for (int i : arr) {
System.out.print(i+" ");
}
}
三角形:
public static void main(String[] args){
for(int i=1;i<=5;i++){
for(int j=5; i<=j; j--)
System.out.print(" ");
for(int j=1; j<=i; j++)
System.out.print("*");
for(int j=1; j System.out.print("*");
System.out.println();
}
}
倒立三角形:
public static void main(String[] args) {
//打印倒立的三角形
for (int m = 1; m <= 4; m++) {
//打印空格
for (int n = 0; n <= m; n++) {
System.out.print(" ");
}
//打印*
for (int x = 1; x <= 7 -2 * (m - 1); x++) {
System.out.print("*");
}
System.out.println();
}
}
回数:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个整数:");
int num = sc.nextInt();
String str1 = num + "";
StringBuilder str2 = new StringBuilder(str1);
str2.reverse();
int count = 0;
for (int i = 0; i < str1.length(); i++) {
if (str1.charAt(i) != str2.charAt(i)) {
System.out.println(str1 + "不是回文数");
break;
} else {
count++;
}
}
if (count == str1.length()) {
System.out.println(str1 + "是回文数");
}
}
素数(质数)(3-100以内的质数):
int j;
boolean flag;
for (int i = 3; i < 100; i++) {
flag = false;
for (j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
flag = true;
break;
}
}
if (flag == false) {
System.out.print(i + " ");
}
}