Advertisement
Guest User

Java Gun Statistics Demo

a guest
Jan 27th, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.08 KB | None | 0 0
  1. package me.tsgs.gunstatistics;
  2.  
  3. import java.math.BigDecimal;
  4. import java.math.RoundingMode;
  5. import java.util.Scanner;
  6.  
  7. public class Stats {
  8.     // Round Method
  9.     public static double round(double value, int places) {
  10.         if (places < 0) {
  11.             throw new IllegalArgumentException();
  12.         } else {
  13.             BigDecimal bd = BigDecimal.valueOf(value);
  14.             bd = bd.setScale(places, RoundingMode.HALF_UP);
  15.             return bd.doubleValue();
  16.         }
  17.     }
  18.  
  19.     // Main screen method
  20.     public static void ms(int lines) {
  21.         if (lines < 0 || lines > 11) {
  22.             throw new IllegalArgumentException();
  23.         } else {
  24.             System.out.print("\nGUN STATS");
  25.             for (int i = 0; i < lines; i++) {
  26.                 System.out.print("\n");
  27.             }
  28.         }
  29.     }
  30.    
  31.     //Litterally just a method to create x number of new lines... (System.out.print("\n");
  32.     public static void nl(int lines) {
  33.         if (lines < 0 || lines > 11) {
  34.             throw new IllegalArgumentException();
  35.         } else {
  36.         for (int i = 0; i < lines; i++) {
  37.             System.out.print("\n");
  38.             }
  39.         }
  40.     }
  41.    
  42.     // Main method A.K.A. The beginning
  43.     public static void main(String[] args) {
  44.         // Create most of the variables
  45.         String name = null;
  46.         String input = null;
  47.         int magcap = 0;//Magazine Capacity
  48.         int mn = 1;//Mag number
  49.         int mnm = 0;//Mag number Max
  50.         int sl = 0;//Shots left
  51.         int sf = 0;//Shots fired
  52.         int bh = 0;//Best hit
  53.         int wh = 10;//Worst hit
  54.         double total = 0;//Total points
  55.         int ch = 0;//Current hit
  56.         int lh = 0;//Last hit
  57.         int[] shots = new int[500];//New array called  'shots' starting with 500 places all at '0' value.
  58.         double avg = 0;//Average hit
  59.         boolean run = false;//Should it run?
  60.         boolean hasrun = false;//Has it run?
  61.         boolean err = false;//Is there an error?
  62.         // Create a new scanner object
  63.         Scanner scanner = new Scanner(System.in);
  64.         ms(11);
  65.         System.out.print("Whats your name? ");
  66.         name = scanner.next();
  67.         ms(10);
  68.         System.out.println("Great " + name + "!");
  69.         System.out.print("How many bullets fit in your mag? ");
  70.         input = scanner.next();
  71.         try {
  72.             magcap = Integer.parseInt(input);
  73.         } catch (NumberFormatException e) {
  74.             System.err.print("ERROR! Invalid input!");
  75.             err = true;
  76.         }
  77.         if (err != true) {
  78.             ms(11);
  79.             System.out.print("Is your mag full? ");
  80.             input = scanner.next();
  81.             input.toLowerCase();
  82.             if (input.equals("yes") || input.startsWith("y")) {
  83.                 sl = magcap;
  84.             } else if (input.equals("no") || input.startsWith("n")) {
  85.                 ms(11);
  86.                 System.out.print("How many bullets are loaded then? ");
  87.                 input = scanner.next();
  88.                 try {
  89.                     sl = Integer.parseInt(input);
  90.                 } catch (NumberFormatException e) {
  91.                     System.err.print("ERROR! Invalid input!");
  92.                     err = true;
  93.                 }
  94.             } else {
  95.                 err = true;
  96.             }
  97.         }
  98.         if (err != true) {
  99.             ms(2);
  100.             System.out.print("Is this information correct?\n" + "\nName: " + name + "\nMagazine capacity: " + magcap
  101.                     + "\nBullets currently loaded: " + sl + "\n\n\n\n\nIs this correct? ");
  102.             input = scanner.next();
  103.             input.toLowerCase();
  104.             if (input.equals("yes") || input.startsWith("y")) {
  105.                 err = false;
  106.             } else {
  107.                 err = true;
  108.             }
  109.         }
  110.         if (err != true) {
  111.             ms(10);
  112.             System.out.print("(yes or no)\nDo you wish to start? ");
  113.             input = scanner.next();
  114.             input.toLowerCase();
  115.             if (input.equals("yes") || input.startsWith("y")) {
  116.                 run = true;
  117.             } else if (input.equals("no") || input.startsWith("n")) {
  118.                 run = false;
  119.             }
  120.         }
  121.         // Main loop
  122.         while (run == true) {
  123.             hasrun = true;// Update the hasrun to true, to tell the program that is has been in the loop at least once
  124.             ms(2);
  125.             System.out.print("Shots fired: " + sf
  126.             + "\nShots left in mag: " + sl
  127.             + "\nBest hit: " + bh
  128.             + "\nWorst hit: " + wh
  129.             + "\nLast hit: " + lh
  130.             + "\nAverage hit: " + avg
  131.             + "\nTotal: " + total);
  132.             System.out.print("\n\n\nWhere did you hit? ");
  133.             input = scanner.next();
  134.             try {
  135.                 ch = Integer.parseInt(input);
  136.             } catch (NumberFormatException e) {
  137.                 err = true;
  138.                 run = false;
  139.                 break;
  140.             }
  141.             // Set a maximum and minimum point avaliable
  142.             if (ch > 10) {
  143.                 ch = 10;
  144.             } else if (ch < 0) {
  145.                 ch = 0;
  146.             }
  147.             total += ch;// Total = total + new point
  148.             lh = ch;// Last point = new point
  149.             ch = 0;// Reset new point
  150.  
  151.             // Array of all the shots
  152.             shots[sf] = lh;
  153.  
  154.             // Set a new best hit
  155.             if (bh < lh) {
  156.                 bh = lh;
  157.             }
  158.             // Set a new worst hit
  159.             if (wh > lh) {
  160.                 wh = lh;
  161.             }
  162.             sf++;
  163.             sl--;
  164.             avg = total / sf;
  165.             avg = round(avg, 1);
  166.         }
  167.         // Check to see if the loop ever ran
  168.         if (hasrun == true) {
  169.             //OUTPUT
  170.             ms(2);
  171.             System.out.print("You shot the following:");
  172.             nl(1);
  173.             int counter = 0;
  174.             int i = 0;
  175.            
  176.             while (sf > magcap) {
  177.                 sf-=magcap;
  178.                 mnm++;
  179.             }
  180.            
  181.             while (counter <= mnm) {
  182.                 System.out.print("Mag " + mn + ": ");
  183.                
  184.                 for (i+=0; i < magcap; i++) {
  185.                     System.out.print(shots[i] + " ");
  186.                 }
  187.                
  188.                 counter++;
  189.                 mn++;
  190.                 nl(1);
  191.             }
  192.  
  193.             scanner.close();
  194.         } else if (err == true) {
  195.             scanner.close();
  196.             System.err.print("ERROR!");
  197.         } else {
  198.             scanner.close();
  199.             ms(11);
  200.             System.out.print("Okay, bye then!");
  201.         }
  202.     }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement