Skip to content

Instantly share code, notes, and snippets.

@bytecodeman
Last active December 4, 2018 12:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bytecodeman/929ef3b803b6fcdcc3ca0e885f47edc8 to your computer and use it in GitHub Desktop.
Save bytecodeman/929ef3b803b6fcdcc3ca0e885f47edc8 to your computer and use it in GitHub Desktop.
CSC-111 HW9 Generate Lucky for Life Numbers
/*
* Name: Prof. Antonio C. Silvestri
* Date: 12/3/2018
* Course Number: CSC-111
* Course Name: Intro to Java Programming
* Problem Number: HW9
* Email: silvestri@stcc.edu
* Description: Generate Lucky for Life Numbers
*/
package luckyforlife;
import java.util.Scanner;
public class LuckyForLife {
private static int[] initArray(int number) {
int list[] = new int[number];
for (int i = 0; i < list.length; i++)
list[i] = i + 1;
return list;
}
private static void shuffle(int[] list) {
for (int i = 0; i < list.length; i++) {
int randomIndex = (int)(Math.random() * list.length);
int temp = list[i];
list[i] = list[randomIndex];
list[randomIndex] = temp;
}
}
private static int[] dealOutRandoms(int[] list, int numberToDealOut) {
int[] luckies = new int[numberToDealOut];
for (int i = 0; i < numberToDealOut; i++)
luckies[i] = list[i];
return luckies;
}
private static void sort(int[] lucky) {
for (int i = 0; i < lucky.length - 1; i++)
for (int j = i + 1; j < lucky.length; j++)
if (lucky[j] < lucky[i]) {
int temp = lucky[i];
lucky[i] = lucky[j];
lucky[j] = temp;
}
}
private static int generateLuckyBall(int max) {
return (int)(Math.random() * max) + 1;
}
private static void printQuickPick(int[] lucky, int luckyBall) {
for (int i = 0; i < lucky.length; i++)
System.out.printf("%3d", lucky[i]);
System.out.printf(" -%3d", luckyBall);
System.out.println();
}
//**********************************************
private static void process(Scanner sc, String args[]) {
int list[] = initArray(43);
shuffle(list);
int lucky[] = dealOutRandoms(list, 5);
sort(lucky);
int luckyBall = generateLuckyBall(43);
printQuickPick(lucky, luckyBall);
}
//**********************************************
private static boolean doThisAgain(Scanner sc, String prompt) {
System.out.print(prompt);
String doOver = sc.nextLine();
return doOver.equalsIgnoreCase("Y");
}
//**********************************************
public static void main(String args[]) {
final String TITLE = "Lucky For Life Quick Pick Generator V1.0";
final String CONTINUE_PROMPT = "Do this again? [y/N] ";
System.out.println("Welcome to " + TITLE);
Scanner sc = new Scanner(System.in);
do {
process(sc, args);
} while (doThisAgain(sc, CONTINUE_PROMPT));
sc.close();
System.out.println("Thank you for using " + TITLE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment