1,2,3,4能組成多少個互不相同且無重複數字的三位數
public static void exp1(){
int sum = ;
for(int i=;i<;i++){
for(int j=;j<;j++){
//判斷十位和百位是否相同
if(j!=i){
for(int k=;k<;k++){
//判斷個位和十位,百位是否相同
if(k!=j&&k!=i){
sum++; System.out.println(""+i+j+k);
}
}
}
}
}
System.out.println("共有"+sum+"個數字");
}
f1,f2兩個檔案的中單詞交叉合并寫入f3。f1,f2單詞以回車或空格分隔
public static void exp2() throws IOException{
String[] s1, s2;
File f1,f2,f3;
FileWriter fw = null;
InputStreamReader reader = null;
try{
f1 = new File("src/week/1.txt");
f2 = new File("src/week/2.txt");
//得到讀取檔案長度的大小的字元數組
char[] cbuf = new char[(int) f1.length()];
//檔案輸入流
reader = new InputStreamReader(new FileInputStream(f1));
//讀取定長
reader.read(cbuf);
//分割字元串用回車或者空格
s1 = new String(cbuf).split("\r\n| ");
cbuf = new char[(int) f2.length()];
reader = new InputStreamReader(new FileInputStream(f2));
reader.read(cbuf);
s2 = new String(cbuf).split("\r\n| ");
f3 = new File("src/week/3.txt");
fw = new FileWriter(f3);
//構造寫入的字元串
StringBuilder sb = new StringBuilder();
int i = ;
//如果s1長度大于s2,恰好構造完成
while(i<s1.length){
sb.append(s1[i]+" ");
if(i<s2.length)
sb.append(s2[i]+" ");
i++;
}
//如果s1<s2,将s2剩下的長度添加到串尾
if(s1.length<s2.length)
{
for(int j=s1.length;j<s2.length;j++)
sb.append(s2[i]+" ");
}
//寫入檔案
fw.write(sb.toString());
//重新整理輸出流,否則寫入不成功
fw.flush();
//從輸入流中跳過并丢棄n個位元組資料 回車占2個位元組
//System.out.println(br.skip(6));
}catch(Exception e){
e.printStackTrace();
}finally{
fw.close();
reader.close();
}
}
“`需要注意的是java的回車是\r\n,如果隻用\n來分割的話,寫入後會出現換行。