Skip to content

Instantly share code, notes, and snippets.

@lbattaglioli2000
Created October 4, 2019 18:03
Show Gist options
  • Save lbattaglioli2000/c15d85c6f4263e8fd9fc54f5010344de to your computer and use it in GitHub Desktop.
Save lbattaglioli2000/c15d85c6f4263e8fd9fc54f5010344de to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class Baseball {
public static void main(String[] args){
// Create an instance of the Scanner class to get user input.
Scanner s = new Scanner(System.in);
// Initalize variables for our information
int singles, doubles, triples, homeRuns, atBats;
String name;
// Prompt the user to start, or stop, the program execution by entering the number of singles.
System.out.println("\nSingles (-1 to end): ");
// Get the number of singles for the player, from the user.
singles = s.nextInt();
// As long as the number of singles is NOT -1, we'll continuously iterate over the code within the while-loop.
while(singles != -1){
// Here we simply ask the user to enter the information and store it in the appropriate variables.
System.out.println("Doubles: ");
doubles = s.nextInt();
System.out.println("Triples: ");
triples = s.nextInt();
System.out.println("Home runs: ");
homeRuns = s.nextInt();
System.out.println("At bats: ");
atBats = s.nextInt();
System.out.println("Player name: ");
s.nextLine(); // clear the input memory buffer!
name = s.nextLine(); // actually get the name of the player from the user!
// we now determine the total number of bases by multiplying:
// - the total number of doubles by 2
// - the total number of triples by 3
// - the total number of home-runs by 4
// and adding them all up.
int totalBases = (singles) + (doubles * 2) + (triples * 3) + (homeRuns * 4);
// we then figure out our sluggingPercentage by taking the totalBases,
// that we figured out in the previous line, and dividing it by the
// total number of atBats. we need to cast atBats to a double however
// so we're not performing integer division and losing the decimals
// from our slugging percentage.
double sluggingPercentage = totalBases / (double) atBats;
// we then figure our the batting average by simply adding up the
// total hits and dividing by the total atBats, which again, must
// be converted to a double so we can do the division without losing
// any precision.
double battingAverage = (singles + doubles + triples + homeRuns) / (double) atBats;
// next we just output the labeled information to the user
System.out.printf("\n" + name + "'s Batting Average: %.3f\n", (battingAverage * 100));
System.out.printf(name + "'s Slugging Percentage: %.3f\n", (sluggingPercentage * 100));
// we check if singles is not equal to -1
if (singles != -1){
// it's not -1 if we're inside of this if statement, so we ask them to start
// the loop all over again. if they enter -1, it'll stop the program.
System.out.println("\nSingles (-1 to end): ");
singles = s.nextInt();
}
}
// all your base are belong to us.
// https://www.youtube.com/watch?v=jQE66WA2s-A
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment