Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Score {
- int incorrect = 0;
- public long startingTime = 0;
- public long endingTime;
- public void scoreWriter(long high) {
- BufferedWriter bw = null;
- BufferedReader br = null;
- try {
- File file = new File("Score.txt");
- if (!file.exists()) {
- FileWriter fw = new FileWriter(file);
- bw = new BufferedWriter(fw);
- for (int i = 1; i <= 3; i++) {
- bw.write("0\n");
- }
- bw.close();
- }
- br = new BufferedReader(new FileReader("Score.txt"));
- long[] scores = new long[3];
- String line = br.readLine();
- int counter = 0;
- while (line != null) {
- long actualScore = Long.parseLong(line);
- scores[counter] = actualScore;
- counter++;
- line = br.readLine();
- }
- long tempValue;
- for (int i = 0; i <= scores.length - 1; i++) {
- if (scores[i] < high) {
- tempValue = scores[i];
- scores[i] = high;
- high = tempValue;
- }
- }
- FileWriter fw = new FileWriter(file);
- bw = new BufferedWriter(fw);
- for (int i = 0; i < scores.length; i++) {
- String update = Long.toString(scores[i]);
- bw.write(update + "\n");
- }
- } catch (IOException ioe) {
- ioe.printStackTrace(System.out);
- } finally {
- try {
- if (bw != null) {
- bw.close();
- }
- if (br != null) {
- br.close();
- }
- } catch (IOException ioe) {
- System.out.println("Error in closing the BufferedWriter");
- ioe.printStackTrace(System.out);
- }
- }
- }
- public String[] scoreReader() {
- BufferedReader br = null;
- String[] scores = new String[3];
- try {
- br = new BufferedReader(new FileReader("myfile.txt"));
- String contentLine = br.readLine();
- int counter = 0;
- while (contentLine != null) {
- contentLine = br.readLine();
- scores[counter] = contentLine;
- counter++;
- }
- return scores;
- } catch (IOException ioe) {
- ioe.printStackTrace(System.out);
- } finally {
- try {
- if (br != null) {
- br.close();
- }
- } catch (IOException ioe) {
- System.out.println("Error in closing the BufferedReader");
- ioe.printStackTrace(System.out);
- }
- }
- return scores;
- }
- public void incAnswer() {
- incorrect++;
- }
- public void timer() {
- if (startingTime == 0) {
- startingTime = System.currentTimeMillis();
- } else {
- endingTime = System.currentTimeMillis() - startingTime;
- }
- }
- public long scoreCalculator(){
- long score = 1000000;
- long negatives = endingTime/5 + incorrect*10000;
- score = score - negatives;
- return score;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement