Skip to content

Instantly share code, notes, and snippets.

@mizucoffee
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mizucoffee/ad6256b62f3c725f5120 to your computer and use it in GitHub Desktop.
Save mizucoffee/ad6256b62f3c725f5120 to your computer and use it in GitHub Desktop.
プログラミング生放送 俳句コンテスト 外部ファイル読み込み ver.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Random;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
public class DataRead_Haiku {
static int n = 0;
static String[] data;
public static void main(String[] args) throws Exception {
try{
File file = new File("HaikuData.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
br.mark(1024);
while(br.readLine() != null){
n++;
}
br.reset();
data = new String[n];
for(int i = 0;i < n;i++){
data[i] = br.readLine();
}
br.close();
}catch(FileNotFoundException e){
System.out.println(e);
}catch(IOException e){
System.out.println(e);
}
int ran = new Random().nextInt(n);//変数ranにnumの数の乱数を振る
String[] strAry = data[ran].split("/");//ranの数字の配列を"/"で3つの配列に分ける。
String a = "" + strAry[0] + "       ";//改行の為にスペースを取る
String b = " " + strAry[1] + "       ";
String c = "  "+ strAry[2] + "       ";
for (int i = 0; i < 9; i++){//繰り返して表示していく。
System.out.print(c.substring(i,i+1));
System.out.print(b.substring(i,i+1));
System.out.println(a.substring(i,i+1));
}
}
}
プログラマ/いのちけずって/今日も行く
プロ生ちゃん/プログラマーの/癒しの子
スパゲッティ/見かけるだけで/寒気する
FFと/聞いて思うは/255
野田さんと/一緒に仕事を/してみたい
できますと/言った直後に/後悔が

このソースはData.txtにはいってるデータを参照して表示するプログラムです。 俳句の形式は 古池や/蛙飛び込む/水の音 のように行ごとにスラッシュを入力すれば、ランダムでどれか表示されます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment