-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBattersUp.java
46 lines (33 loc) · 1.19 KB
/
BattersUp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//BattersUp.java
import java.util.Scanner; //Needed for the Scanner class
import java.text.*; //Needed for Decimal Formatting
/**
This program calculates a batting average based on input from
the user.
*/
public class BattersUp
{
public static void main(String[] args)
{
DecimalFormat df = new DecimalFormat(".###");
double atBats; // Number of at bats
double hits; // Number of hits
double average; // Batting average
//Create a Scanner object to read input.
Scanner keyboard = new Scanner(System.in);
// Get the number of at bats.
System.out.print("How many at bats have you had? ");
atBats = keyboard.nextDouble();
// Get the number of hits.
System.out.print("How many hits have you attained? ");
hits = keyboard.nextDouble();
// Calculate the batting average
average = (hits / atBats);
//Print the information
System.out.println("Your total at bats: " + atBats);
System.out.println("The number of hits you've attained: " + hits);
System.out.println("Your batting average is " + df.format(average));
if (average > 400)
System.out.print("You're going to the Allstars!");
}
}