Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.tsgs.gunstatistics;
- import java.math.BigDecimal;
- import java.math.RoundingMode;
- import java.util.Scanner;
- public class Stats {
- // Round Method
- public static double round(double value, int places) {
- if (places < 0) {
- throw new IllegalArgumentException();
- } else {
- BigDecimal bd = BigDecimal.valueOf(value);
- bd = bd.setScale(places, RoundingMode.HALF_UP);
- return bd.doubleValue();
- }
- }
- // Main screen method
- public static void ms(int lines) {
- if (lines < 0 || lines > 11) {
- throw new IllegalArgumentException();
- } else {
- System.out.print("\nGUN STATS");
- for (int i = 0; i < lines; i++) {
- System.out.print("\n");
- }
- }
- }
- //Litterally just a method to create x number of new lines... (System.out.print("\n");
- public static void nl(int lines) {
- if (lines < 0 || lines > 11) {
- throw new IllegalArgumentException();
- } else {
- for (int i = 0; i < lines; i++) {
- System.out.print("\n");
- }
- }
- }
- // Main method A.K.A. The beginning
- public static void main(String[] args) {
- // Create most of the variables
- String name = null;
- String input = null;
- int magcap = 0;//Magazine Capacity
- int mn = 1;//Mag number
- int mnm = 0;//Mag number Max
- int sl = 0;//Shots left
- int sf = 0;//Shots fired
- int bh = 0;//Best hit
- int wh = 10;//Worst hit
- double total = 0;//Total points
- int ch = 0;//Current hit
- int lh = 0;//Last hit
- int[] shots = new int[500];//New array called 'shots' starting with 500 places all at '0' value.
- double avg = 0;//Average hit
- boolean run = false;//Should it run?
- boolean hasrun = false;//Has it run?
- boolean err = false;//Is there an error?
- // Create a new scanner object
- Scanner scanner = new Scanner(System.in);
- ms(11);
- System.out.print("Whats your name? ");
- name = scanner.next();
- ms(10);
- System.out.println("Great " + name + "!");
- System.out.print("How many bullets fit in your mag? ");
- input = scanner.next();
- try {
- magcap = Integer.parseInt(input);
- } catch (NumberFormatException e) {
- System.err.print("ERROR! Invalid input!");
- err = true;
- }
- if (err != true) {
- ms(11);
- System.out.print("Is your mag full? ");
- input = scanner.next();
- input.toLowerCase();
- if (input.equals("yes") || input.startsWith("y")) {
- sl = magcap;
- } else if (input.equals("no") || input.startsWith("n")) {
- ms(11);
- System.out.print("How many bullets are loaded then? ");
- input = scanner.next();
- try {
- sl = Integer.parseInt(input);
- } catch (NumberFormatException e) {
- System.err.print("ERROR! Invalid input!");
- err = true;
- }
- } else {
- err = true;
- }
- }
- if (err != true) {
- ms(2);
- System.out.print("Is this information correct?\n" + "\nName: " + name + "\nMagazine capacity: " + magcap
- + "\nBullets currently loaded: " + sl + "\n\n\n\n\nIs this correct? ");
- input = scanner.next();
- input.toLowerCase();
- if (input.equals("yes") || input.startsWith("y")) {
- err = false;
- } else {
- err = true;
- }
- }
- if (err != true) {
- ms(10);
- System.out.print("(yes or no)\nDo you wish to start? ");
- input = scanner.next();
- input.toLowerCase();
- if (input.equals("yes") || input.startsWith("y")) {
- run = true;
- } else if (input.equals("no") || input.startsWith("n")) {
- run = false;
- }
- }
- // Main loop
- while (run == true) {
- hasrun = true;// Update the hasrun to true, to tell the program that is has been in the loop at least once
- ms(2);
- System.out.print("Shots fired: " + sf
- + "\nShots left in mag: " + sl
- + "\nBest hit: " + bh
- + "\nWorst hit: " + wh
- + "\nLast hit: " + lh
- + "\nAverage hit: " + avg
- + "\nTotal: " + total);
- System.out.print("\n\n\nWhere did you hit? ");
- input = scanner.next();
- try {
- ch = Integer.parseInt(input);
- } catch (NumberFormatException e) {
- err = true;
- run = false;
- break;
- }
- // Set a maximum and minimum point avaliable
- if (ch > 10) {
- ch = 10;
- } else if (ch < 0) {
- ch = 0;
- }
- total += ch;// Total = total + new point
- lh = ch;// Last point = new point
- ch = 0;// Reset new point
- // Array of all the shots
- shots[sf] = lh;
- // Set a new best hit
- if (bh < lh) {
- bh = lh;
- }
- // Set a new worst hit
- if (wh > lh) {
- wh = lh;
- }
- sf++;
- sl--;
- avg = total / sf;
- avg = round(avg, 1);
- }
- // Check to see if the loop ever ran
- if (hasrun == true) {
- //OUTPUT
- ms(2);
- System.out.print("You shot the following:");
- nl(1);
- int counter = 0;
- int i = 0;
- while (sf > magcap) {
- sf-=magcap;
- mnm++;
- }
- while (counter <= mnm) {
- System.out.print("Mag " + mn + ": ");
- for (i+=0; i < magcap; i++) {
- System.out.print(shots[i] + " ");
- }
- counter++;
- mn++;
- nl(1);
- }
- scanner.close();
- } else if (err == true) {
- scanner.close();
- System.err.print("ERROR!");
- } else {
- scanner.close();
- ms(11);
- System.out.print("Okay, bye then!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement