Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package assignment;
- //Mitchell Palermo
- //06.11.2019
- /*this program asks the user for a compass bearing between 0 and 359, the program will then output
- the direction the bearing is facing as:
- North, North/East, East, South/East, South, South/West, West, or North/West
- */
- //this imports the Scanner from the java library
- import java.util.Scanner;
- public class CompassAssignment
- {
- public static void main(String[] args)
- {
- //--------------Variable Declaration----------------------
- //this declares the scanner reference variable as "keyboard"
- Scanner keyboard = new Scanner(System.in);
- //declaration of an input variable used to store the users input bearing
- int input;
- //-------------------Input--------------------------------
- //this asks the user to enter an input bearing between 0 and 359
- System.out.print("Please enter a bearing between 0 and 359: ");
- //this collects the user input and stores it in the "input" integer type variable
- input = keyboard.nextInt();
- //-------------------Processing & Output-------------------
- //Primary if statement
- //this if statement sorts invalid bearings from valid ones
- if (input >= 0 && input < 360)
- {
- //Secondary if statement
- //valid input bearings are then sorted producing output displaying its
- //correct directional equivalent
- if (input >= 0 && input < 45 )
- {
- //this bearing is North
- System.out.println("A bearing of "+input+" means you are heading North.");
- }
- else if (input == 45)
- {
- //this bearing is precisely N/E
- System.out.println("A bearing of "+input+" means you are heading North/East.");
- }
- else if (input > 45 && input < 135)
- {
- //this bearing is East
- System.out.println("A bearing of "+input+" means you are heading East.");
- }
- else if (input == 135)
- {
- //this bearing is precisely S/E
- System.out.println("A bearing of "+input+" means you are heading South/East.");
- }
- else if (input > 135 && input < 225)
- {
- //this bearing is South
- System.out.println("A bearing of "+input+" means you are heading South.");
- }
- else if (input == 225)
- {
- //this bearing is precisely S/W
- System.out.println("A bearing of "+input+" means you are heading South/West.");
- }
- else if (input > 225 && input < 315)
- {
- //this bearing is West
- System.out.println("A bearing of "+input+" means you are heading West.");
- }
- else if (input == 315)
- {
- //this bearing is precisely N/W
- System.out.println("A bearing of "+input+" means you are heading North/West.");
- }
- else
- {
- //if a bearing is to fall under the else of this nested if statement the bearing must be
- // 315 < input < 360
- System.out.println("A bearing of "+input+" means you are heading North.");
- }
- }
- else
- {
- // invalid bearings will produce an output of this error message prompting the user to
- //restart the program and enter a valid bearing
- System.out.println("That is not a valid bearing. Please restart the program and enter "
- + "a bearing between 0 and 359 degrees.");
- }
- //this closes the keyboard scanner so it can no longer accept input
- keyboard.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement