Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. public class Score {
  2.  
  3. int incorrect = 0;
  4. public long startingTime = 0;
  5. public long endingTime;
  6.  
  7. public void scoreWriter(long high) {
  8.  
  9. BufferedWriter bw = null;
  10.  
  11. BufferedReader br = null;
  12.  
  13. try {
  14.  
  15. File file = new File("Score.txt");
  16.  
  17. if (!file.exists()) {
  18.  
  19. FileWriter fw = new FileWriter(file);
  20.  
  21. bw = new BufferedWriter(fw);
  22.  
  23. for (int i = 1; i <= 3; i++) {
  24.  
  25. bw.write("0\n");
  26.  
  27. }
  28.  
  29. bw.close();
  30.  
  31. }
  32.  
  33. br = new BufferedReader(new FileReader("Score.txt"));
  34.  
  35. long[] scores = new long[3];
  36. String line = br.readLine();
  37. int counter = 0;
  38. while (line != null) {
  39.  
  40. long actualScore = Long.parseLong(line);
  41. scores[counter] = actualScore;
  42. counter++;
  43. line = br.readLine();
  44. }
  45.  
  46. long tempValue;
  47. for (int i = 0; i <= scores.length - 1; i++) {
  48.  
  49. if (scores[i] < high) {
  50. tempValue = scores[i];
  51. scores[i] = high;
  52. high = tempValue;
  53. }
  54. }
  55.  
  56. FileWriter fw = new FileWriter(file);
  57.  
  58. bw = new BufferedWriter(fw);
  59.  
  60. for (int i = 0; i < scores.length; i++) {
  61.  
  62. String update = Long.toString(scores[i]);
  63. bw.write(update + "\n");
  64. }
  65.  
  66. } catch (IOException ioe) {
  67.  
  68. ioe.printStackTrace(System.out);
  69.  
  70. } finally {
  71.  
  72. try {
  73.  
  74. if (bw != null) {
  75.  
  76. bw.close();
  77.  
  78. }
  79.  
  80. if (br != null) {
  81.  
  82. br.close();
  83.  
  84. }
  85.  
  86. } catch (IOException ioe) {
  87.  
  88. System.out.println("Error in closing the BufferedWriter");
  89.  
  90. ioe.printStackTrace(System.out);
  91.  
  92. }
  93.  
  94. }
  95.  
  96. }
  97.  
  98. public String[] scoreReader() {
  99.  
  100. BufferedReader br = null;
  101.  
  102. String[] scores = new String[3];
  103.  
  104. try {
  105.  
  106. br = new BufferedReader(new FileReader("myfile.txt"));
  107.  
  108. String contentLine = br.readLine();
  109.  
  110. int counter = 0;
  111.  
  112. while (contentLine != null) {
  113.  
  114. contentLine = br.readLine();
  115.  
  116. scores[counter] = contentLine;
  117.  
  118. counter++;
  119.  
  120. }
  121.  
  122. return scores;
  123.  
  124. } catch (IOException ioe) {
  125.  
  126. ioe.printStackTrace(System.out);
  127.  
  128. } finally {
  129.  
  130. try {
  131.  
  132. if (br != null) {
  133.  
  134. br.close();
  135.  
  136. }
  137.  
  138. } catch (IOException ioe) {
  139.  
  140. System.out.println("Error in closing the BufferedReader");
  141.  
  142. ioe.printStackTrace(System.out);
  143.  
  144. }
  145.  
  146. }
  147.  
  148. return scores;
  149.  
  150. }
  151.  
  152. public void incAnswer() {
  153.  
  154. incorrect++;
  155.  
  156. }
  157.  
  158. public void timer() {
  159.  
  160. if (startingTime == 0) {
  161.  
  162. startingTime = System.currentTimeMillis();
  163.  
  164. } else {
  165.  
  166. endingTime = System.currentTimeMillis() - startingTime;
  167.  
  168. }
  169.  
  170. }
  171.  
  172. public long scoreCalculator(){
  173.  
  174. long score = 1000000;
  175. long negatives = endingTime/5 + incorrect*10000;
  176. score = score - negatives;
  177. return score;
  178.  
  179. }
  180.  
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement