Q
1~100000の乱数を生成するゲームを作成し、そのハイスコアをファイルに書き込むことで管理せよ。
はじめてゲームをするときのハイスコアは0
記録が出た時の日付も記録すること。
[実行例]
(初回時)
****乱数ゲーム****
HighScore 0
やりますかyes…y no…n>y
33081
new Record
やりますかyes…y no…n>y
79048
new Record
やりますかyes…y no…n>y
63973
やりますかyes…y no…n>n
アプリを終了します
(二回目以降)
****乱数ゲーム****
HighScore 79048 2018/07/31
やりますかyes…y no…n>y
31186
やりますかyes…y no…n>y
29340
やりますかyes…y no…n>y
22127
やりますかyes…y no…n>y
5352
やりますかyes…y no…n>y
16096
やりますかyes…y no…n>y
80957
new Record
やりますかyes…y no…n>n
アプリを終了します
[解答例]
Score.java
01 | public class HighScore { |
04 | public HighScore( int score,String date) { |
09 | public String toString() { |
10 | return this .score+ " " + this .date; |
12 | public String toCSV() { |
13 | return this .score+ "," + this .date; |
15 | public int getScore() { |
アプリケーションクラス
001 | import java.io.BufferedReader; |
002 | import java.io.BufferedWriter; |
003 | import java.io.FileInputStream; |
004 | import java.io.FileNotFoundException; |
005 | import java.io.FileOutputStream; |
006 | import java.io.IOException; |
007 | import java.io.InputStreamReader; |
008 | import java.io.OutputStreamWriter; |
009 | import java.io.UnsupportedEncodingException; |
010 | import java.text.SimpleDateFormat; |
011 | import java.util.Date; |
012 | import java.util.Random; |
013 | import java.util.Scanner; |
015 | public class HighScoreGame { |
017 | public static void main(String[] args) { |
018 | final String FILE= "score.csv" ; |
019 | final int MAX= 100000 ; |
020 | Scanner scan = new Scanner(System.in); |
021 | Random rand= new Random(); |
022 | boolean isHighScore= false ; |
023 | System.out.println( "****乱数ゲーム****" ); |
024 | HighScore hs=readFile(FILE); |
027 | System.out.println( "HighScore 0" ); |
030 | System.out.println( "HighScore " +hs); |
031 | highScore=hs.getScore(); |
034 | System.out.print( "やりますかyes...y no...n>" ); |
035 | String select=scan.next(); |
036 | if (select.equals( "n" )) { |
037 | System.out.println( "アプリを終了します" ); |
039 | Date today= new Date(); |
040 | SimpleDateFormat sdf= new SimpleDateFormat( "yyyy/MM/dd" ); |
041 | String date=sdf.format(today); |
042 | hs= new HighScore(highScore,date); |
043 | writeRecord(FILE,hs); |
048 | int no=rand.nextInt(MAX)+ 1 ; |
049 | System.out.println(no); |
053 | System.out.println( "new Record" ); |
058 | public static HighScore readFile(String file) { |
060 | BufferedReader br= null ; |
062 | FileInputStream fis= new FileInputStream(file); |
063 | InputStreamReader isr= new InputStreamReader(fis, "UTF-8" ); |
064 | br= new BufferedReader(isr); |
065 | String line=br.readLine(); |
066 | if (line!= null && !line.equals( "" )) { |
067 | String[] data=line.split( "," ); |
068 | hs= new HighScore(Integer.parseInt(data[ 0 ]),data[ 1 ]); |
070 | } catch (FileNotFoundException e) { |
072 | } catch (UnsupportedEncodingException e) { |
074 | } catch (IOException e) { |
080 | public static void writeRecord(String fileName,HighScore hs) { |
081 | BufferedWriter bw= null ; |
083 | FileOutputStream fos= new FileOutputStream(fileName); |
084 | OutputStreamWriter osw= new OutputStreamWriter(fos, "UTF-8" ); |
085 | bw= new BufferedWriter(osw); |
086 | bw.append(hs.toCSV()); |
088 | } catch (FileNotFoundException e) { |
090 | } catch (UnsupportedEncodingException e) { |
093 | } catch (IOException e) { |
099 | } catch (IOException e) { |
(参考)
ファイルオブジェクトを使った例
001 | import java.io.BufferedReader; |
002 | import java.io.BufferedWriter; |
004 | import java.io.FileInputStream; |
005 | import java.io.FileNotFoundException; |
006 | import java.io.FileOutputStream; |
007 | import java.io.IOException; |
008 | import java.io.InputStreamReader; |
009 | import java.io.OutputStreamWriter; |
010 | import java.io.UnsupportedEncodingException; |
011 | import java.text.SimpleDateFormat; |
012 | import java.util.Date; |
013 | import java.util.Random; |
014 | import java.util.Scanner; |
016 | public class HighScoreGame { |
018 | public static void main(String[] args) { |
019 | final String FILE= "score2.csv" ; |
020 | final int MAX= 100000 ; |
021 | Scanner scan = new Scanner(System.in); |
022 | Random rand= new Random(); |
024 | boolean isHighScore= false ; |
026 | System.out.println( "****乱数ゲーム****" ); |
027 | File file= new File(FILE); |
030 | System.out.println( "HighScore " +hs); |
031 | highScore=hs.getScore(); |
033 | System.out.println( "HighScore 0" ); |
036 | System.out.print( "やりますかyes...y no...n>" ); |
037 | String select=scan.next(); |
038 | if (select.equals( "n" )) { |
039 | System.out.println( "アプリを終了します" ); |
041 | Date today= new Date(); |
042 | SimpleDateFormat sdf= new SimpleDateFormat( "yyyy/MM/dd" ); |
043 | String date=sdf.format(today); |
044 | hs= new HighScore(highScore,date); |
045 | writeRecord(FILE,hs); |
050 | int no=rand.nextInt(MAX)+ 1 ; |
051 | System.out.println(no); |
055 | System.out.println( "new Record" ); |
059 | public static HighScore readFile(File file) { |
061 | BufferedReader br= null ; |
063 | FileInputStream fis= new FileInputStream(file); |
064 | InputStreamReader isr= new InputStreamReader(fis, "UTF-8" ); |
065 | br= new BufferedReader(isr); |
066 | String line=br.readLine(); |
067 | String[] data=line.split( "," ); |
068 | hs= new HighScore(Integer.parseInt(data[ 0 ]),data[ 1 ]); |
069 | } catch (FileNotFoundException e) { |
071 | } catch (UnsupportedEncodingException e) { |
073 | } catch (IOException e) { |
078 | public static void writeRecord(String fileName,HighScore hs) { |
079 | BufferedWriter bw= null ; |
081 | FileOutputStream fos= new FileOutputStream(fileName); |
082 | OutputStreamWriter osw= new OutputStreamWriter(fos, "UTF-8" ); |
083 | bw= new BufferedWriter(osw); |
084 | bw.append(hs.toCSV()); |
086 | } catch (FileNotFoundException e) { |
088 | } catch (UnsupportedEncodingException e) { |
091 | } catch (IOException e) { |
097 | } catch (IOException e) { |
コメント