String有l(wèi)ength的屬性嗎?數(shù)組有l(wèi)ength的方法嗎? String只有l(wèi)ength的方法,,數(shù)組有l(wèi)ength的屬性
package com.faust.java.system.services;
import java.util.Random;
public class Array {
public static void main(String[] args) { // TODO Auto-generated method stub Random r = new Random(); for (int i = 0; i < 4; i++) { int type = r.nextInt(3); // if (type == 0) { // System.out.print(r.nextInt(10)); // } else if (type == 1) { // System.out.print((char) (r.nextInt(26) + 97)); // } else { // System.out.print((char) (r.nextInt(26) + 65)); // } switch (type) { case 0: System.out.print(r.nextInt(10)); break; case 1: System.out.print((char) (r.nextInt(26) + 97)); break; case 2: System.out.print((char) (r.nextInt(26) + 65)); break;
} }
} }
冒泡排序
package com.faust.java.supplier.dao;
import java.util.Arrays; public class demo1 {
public static void main(String[] args) { // TODO Auto-generated method stub int[] arr = { 99, 5, 8, 9, 55 }; for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - i - 1; j++) { if (arr[j + 1] < arr[j]) { int temp = arr[j + 1]; arr[j + 1] = arr[j]; arr[j] = temp; for (int k : arr) { System.out.println(k); } System.out.println(Arrays.toString(arr)); }
} }
}
}
選擇排序
package com.faust.java.supplier.dao;
import java.util.Arrays;
public class demo2 {
public static void main(String[] args) { // TODO Auto-generated method stub} int[] arr = { 99, 5, 9, 7, 55 }; for (int i = 0; i < arr.length-1; 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; } } }System.out.println(Arrays.toString(arr)); }
}
輸入分?jǐn)?shù)進(jìn)行大小排列
package com.faust.java.supplier.services;
import java.util.Arrays; import java.util.Scanner;
public class Demo1 {
public static void main(String[] args) { // TODO Auto-generated method stub // 1 使用輸入分?jǐn)?shù)進(jìn)行冒泡排序,int[] score = new int[5]; 對(duì)數(shù)組的元素進(jìn)行賦值,,最后進(jìn)行排序 // 1.進(jìn)行分?jǐn)?shù)輸入進(jìn)控制臺(tái),,使用scanner Scanner sc = new Scanner(System.in); System.out.print("請(qǐng)輸入分?jǐn)?shù):"); int[] arr = new int[5]; // 進(jìn)行循環(huán)輸入分?jǐn)?shù) for (int i = 0; i < arr.length; i++) { int score = sc.nextInt(); System.out.print("請(qǐng)繼續(xù)輸入分?jǐn)?shù):"); // 2.將輸入的分?jǐn)?shù)進(jìn)行保存,放入數(shù)組中 arr[i] = score; } // 3.將保存好的數(shù)組進(jìn)行冒泡排序 for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - i - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } System.out.println(Arrays.toString(arr)); }
}
產(chǎn)生隨機(jī)數(shù)
package com.faust.java.system.dao;
import java.util.Random;
public class RandomDemo {
public static void main(String[] args) { // TODO Auto-generated method stub Random ran =new Random(); for (int j = 0; j <4; j++) { for (int k = 0; k <4; k++) { int i = ran.nextInt(10); System.out.print(i); } System.out.println(); } }
}
|