Skip to content

Instantly share code, notes, and snippets.

@LordNairu
Last active August 29, 2015 14:08
Show Gist options
  • Save LordNairu/caa2d399c5b4d17c12df to your computer and use it in GitHub Desktop.
Save LordNairu/caa2d399c5b4d17c12df to your computer and use it in GitHub Desktop.
package uk.ac.qub.programming1p1;
// Importing scanner for user input
import java.util.Scanner;
public class DingDongNumberPractical {
// Declaring constants which limit user input and control output
public static final int MIN_INPUT = 50;
public static final int MAX_INPUT = 100;
public static final int DING_NUMBER = 7;
public static final int DONG_NUMBER = 5;
/**
* Niall Collins - 40002617
* A method that prompts the user for an input from 50-100 and then counts down from that
* input to the number 1. Editing multiples of 7 and 5 to Ding and Dong respectively.
* Multiples both are converted to DingDong.
* Method is looped to allow for multiple plays.
* @param args
*/
public static void main(String[] args) {
// Declaring Variables
int userInput;
int playAgain;
userInput = 0;
playAgain = 0;
// Creating Scanner called scanner
Scanner scanner = new Scanner(System.in);
// Boolean play again check
Boolean invalidInput = false;
// Do..while loop to allow multiple plays
do {
// Prompting the user for a numerical input and preventing entries outside specified range
do {
System.out.printf("Please input a number between %s and %s: \n",MIN_INPUT,MAX_INPUT);
userInput = scanner.nextInt();
// Ensuring number is within expected field
} while ((userInput < MIN_INPUT) || (userInput > MAX_INPUT));
// Creating a for loop to decrement userInput to 1
for (int counter = userInput; counter >= 1; counter--){
// Instructing the program how to deal with pertinent multiples
if ((counter % DING_NUMBER == 0) && (counter % DONG_NUMBER ==0)){
System.out.println("DingDong");
} else if (counter % DING_NUMBER == 0){
System.out.println("Ding");
} else if (counter% DONG_NUMBER == 0){
System.out.println("Dong");
} else System.out.println(+counter);
}// End of for loop
// Checking if the user wishes to play again (looped to prevent invalid entries)
do {
System.out.println("\nWould you like to play again?\n1. Yes\t2. No");
playAgain = scanner.nextInt();
if ((playAgain == 1) || (playAgain ==2)){
invalidInput = false;
} else {
System.out.println("Sorry, input invalid!");
invalidInput = true;}
} while (invalidInput);
// Closing do..while loop which allows the program to close only when the user selects 2
} while ((playAgain != 2));
System.out.println("Goodbye - program terminated");
System.exit(0);
// Closing scanner
scanner.close();
}// end of method
}// end of class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment