カテキョで英単語の小テストを自動的に作るプログラム。
最初にCで作ろうとしたけど文字列処理でちょっと挫折したので(ぉ Javaで。
何も考えずに作ったからカプセル化どころか汚すぎるのは仕様ですw
package word_test; import java.io.*; import java.util.ArrayList; public class Main { public static void main(String[] args){ int word_num = 30; int min = 0; int max = 0; BufferedReader fp = null; try { fp = new BufferedReader(new FileReader("target1800.txt")); } catch (FileNotFoundException e) { System.out.println("file open error."); System.exit(1); } PrintWriter pw = null; try { pw = new PrintWriter (new BufferedWriter(new FileWriter("target.txt"))); } catch (IOException e) { System.out.println("file open error."); System.exit(1); } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("input min = "); try { min = Integer.parseInt(br.readLine()); } catch (NumberFormatException e2) { System.out.println("NumberFormatException."); System.exit(1); } catch (IOException e2) { System.out.println("IOException."); System.exit(1); } System.out.print("input max = "); try { max= (Integer.parseInt(br.readLine())+1); } catch (NumberFormatException e2) { System.out.println("NumberFormatException."); System.exit(1); } catch (IOException e2) { System.out.println("IOException."); System.exit(1); } word_num = (word_num<(max-min))? word_num:(max-min); ArrayList array = new ArrayList(); String str = null; while(true){ try { str = fp.readLine(); } catch (IOException e1) { System.out.println("Error at readLine."); System.exit(1); } if(str==null)break; array.add(str); } int size = array.size(); int[] already = new int[word_num]; for(int i=0;i<word_num;i++){ already[i] = -1; } for(int i=0;i<word_num;i++){ int random = myRandom(max,min); if(isUsed(random,already,word_num)){ i--; // System.out.println(random); continue; } already[i] = random; pw.println("("+(i+1)+")"+array.get(random) + "\t(\t\t\t)"+"\t("+random+")"); already[i] = random; } System.out.println("exit success."); pw.close(); } public static int myRandom(int max, int min){ return (int)(Math.random()*(max-min))+min; } public static boolean isUsed(int x,int[] already,int word_num){ for(int i=0;i<word_num;i++){ if(already[i]==x)return true; } return false; } }