StringBuffer
public class Demo { public static void main(String[] args) { StringBuffer buf = new StringBuffer(); for(int x = 'a'; x <'z'; x ++){ buf.append((char) x); } buf.reverse().delete(0, 5); System.out.println(buf); }}随机数组
package cn.cccc.demo;import java.util.Arrays;import java.util.Random;class NumberFactory{ private static Random random = new Random();
public static int [] create(int len ){ int data [] = new int [len]; int foot = 0; while(foot < data.length){ int num = random.nextInt(30); data[foot++] = num; } return data; }7 collapsed lines
}public class Demo { public static void main(String[] args) { int result [] = NumberFactory.create(5); System.out.println(Arrays.toString(result)); }}抛硬币检测
package cn.cccc.demo;import java.util.Arrays;import java.util.Random;class Coin { private static Random random = new Random(); private int front; private int back; public void throwCoin(int count){ for(int x= 0; x < count ; x++){ int side = random.nextInt(2); if(side == 0 ){ this.front++; }else { this.back++; }16 collapsed lines
} } public int getFront(){ return this.front; } public int getBack(){ return this.back; }}public class Demo { public static void main(String[] args) { Coin coin = new Coin(); coin.throwCoin(1000); System.out.println("正面的次数"+coin.getFront()+",背面的次数"+coin.getBack()); }}