Friend (experienced in programming) "Did you start programming?"
I "Yeah"
Friend "Then try making programming that will determine the numbers for Loto 6"
I can afford it : wink: "
Lotto 6 is a number selection type lottery that selects 6 different numbers from 1 to 43.
As the title says, I'm a beginner in programming, so let's write a code that makes it super easy to select lottery 6 numbers in Java! It is an article of. Of course, we don't scrape past winning numbers. ~~ What is scraping? ~~
It looks like a struggle, so I hope you will remember when you were a beginner and see it with warm eyes.
I will write the code immediately.
I "Well, first make an array to store 6 numbers ..." I said, "Of course, the Random class is indispensable because the numbers from 1 to 43 are randomly selected."
int[] numbers = new int[6];
for (int i = 0; i < numbers.length; i++) {
Random random = new Random();
numbers[i] = random.nextInt(43)+1;
}
I said, "Oh, isn't it complete? Programming is easy : sunglasses:"
I "execute"
result
39
18
7
18
34
11
I "Ah ... that's right ... : sob:"
Obviously, this will cover the numbers. As mentioned above, Lotto 6 has 6 different numbers, so it cannot be said that it is completed.
I said, "Then, if you suffer, you have to reselect it. What should I do?"
I "push it or : muscle:"
do {
for (int i = 0; i < numbers.length; i++) {
Random random = new Random();
numbers[i] = random.nextInt(43)+1;
}
} while (numbers[0] == numbers[1] || numbers[0] == numbers[2] || numbers[0] == numbers[3] ||
numbers[0] == numbers[4] || numbers[0] == numbers[5] || numbers[1] == numbers[2] ||
numbers[1] == numbers[3] || numbers[1] == numbers[4] || numbers[1] == numbers[5] ||
numbers[2] == numbers[3] || numbers[2] == numbers[4] || numbers[2] == numbers[5] ||
numbers[3] == numbers[4] || numbers[3] == numbers[5] || numbers[4] == numbers[5]);
Complete. I got what I was looking for. The numbers are never covered.
I "Look at it ~ I was able to ~ : relaxed:"
Friend "Oh, it was quick. Which one ...?"
friend"······"
Friend "You ..."
I said, "Well, have I done something again? : Triumph:"
My friend "Ba! Ka! Ya! Ro! U!"
Friend "What is programming! You don't have to move! ~~ (preaching) ~~"
I ": dizzy_face: : dizzy_face: : dizzy_face:"
Proposal 1 has become a crap.
I "I don't know what it is, but I got angry"
I said, "But it was a hassle when writing, and it doesn't feel like the programming I envision."
I said, "First, let's sort out the flow. What I want to do is check if the numbers in the array are covered as I wrote earlier. If so, change one of the numbers. And check again from the beginning. "
I said, "Well, should I make a method to check if the 0th to 4th numbers in the array overlap with other numbers !?"
I "Create a method to get a random number for the time being and put it in an array ..."
int[] numbers = new int[6];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = getNumbers();
}
private static int getNumber() {
Random random = new Random();
return random.nextInt(43)+1;
}
I "Let's make a check method"
private static void checkNumber0(int numbers[]) {
for(int i = 1;i < numbers.length;i++) {
if(numbers[0] == numbers[i]) {
numbers[i] = getNumber();
checkNumber0(numbers);
}
}
checkNumber1(numbers);
}
private static void checkNumber1(int numbers[]) {
for(int i = 2;i < numbers.length;i++) {
if(numbers[1] == numbers[i]) {
numbers[i] = getNumber();
checkNumber0(numbers);
}
}
checkNumber2(numbers);
}
private static void checkNumber2(int numbers[]) {
for(int i = 3;i < numbers.length;i++) {
if(numbers[2] == numbers[i]) {
numbers[i] = getNumber();
checkNumber0(numbers);
}
}
checkNumber3(numbers);
}
private static void checkNumber3(int numbers[]) {
for(int i = 4;i < numbers.length;i++) {
if(numbers[3] == numbers[i]) {
numbers[i] = getNumber();
checkNumber0(numbers);
}
}
checkNumber4(numbers);
}
private static void checkNumber4(int numbers[]) {
for(int i = 5;i < numbers.length;i++) {
if(numbers[4] == numbers[i]) {
numbers[i] = getNumber();
checkNumber0(numbers);
}
}
}
I "done!"
I said, "But this isn't longer than before ...? If I show this, I'll get angry again ..."
I said, "The contents of checkNumber 〇 () are similar programming, so I wonder if they can be combined into one."
Think calmly here. The different methods have a lot in common. It seems that common items can be extracted even if the numbers are different.
I "I think I should program this!"
private static void checkNumbers(int index, int[] numbers) {
for (int i = index + 1; i < numbers.length; i++) {
if (numbers[index] == numbers[i]) {
numbers[i] = getNumbers();
checkNumbers(0, numbers);
}
}
index++;
if (index < numbers.length - 1) {
checkNumbers(index, numbers);
}
}
Complete. It's much cleaner than before. The sound of leveling up Dragon Quest is reproduced in the brain.
I said, "It's all done, let's show it to a friend ~ : smirk:"
・ ・ ・
Friend "Hmm. The amount of code itself may not have changed much, but now it's quite versatile. This will make it easier even if there are changes in specifications."
I "passed this !? : satisfied:"
A friend of mine, "Well, for a beginner, it has the makurakotoba. Even if it passes, it's a point."
I said, "Well, I did my best. Or can I write it more easily?"
Friend "Oh, I'll call it more concisely than this. Please do your best to think about it."
I ": dizzy_face: : dizzy_face: : dizzy_face:"
Plan 2 didn't go unnoticed, but my friend gave me only a good score. I want to make a program that will surprise my friends when I get here.
I "what should I do?"
If you organize the flow here
I said, "Hmm. It may be a little wasteful."
I said, "Is it possible to make it more concise by checking if the numbers that were put in until then are covered when assigning variables to the array in [2]?"
for(int i = 0;i < numbers.length;i++) {
numbers[i] = getNumber();
for(int j = 0 ; j < i ;j++) {
if(numbers[j] == numbers[i]) {
numbers[i] = getNumber();
}
}
}
It's not completed ... Naturally the second time
numbers[i]=getNumber();
And there is a possibility that the numbers will be covered again.
I said, "Hmm. It seems that you should make this a method like Plan 2 and if if (true), start over from the beginning ..."
I said "From scratch ...? : Flushed :! No, I just need to reduce it!
for(int i = 0;i < numbers.length;i++) {
numbers[i] = getNumber();
for(int j = 0 ; j < i ;j++) {
if(numbers[j] == numbers[i]) {
i--;
break;
}
}
}
I "Decrement ...! By the way, there was something like this."
I said, "Anyway, I can say that this is the final completion. I was able to write beautiful code without waste! : Blush:"
Loto6
public class Loto6{
public static void main(String[] args) {
int[] numbers = new int[6];
for(int i = 0;i < numbers.length;i++) {
numbers[i] = getNumber();
for(int j = 0 ; j < i ;j++) {
if(numbers[j] == numbers[i]) {
i--;
break;
}
}
}
for (int i : numbers) {
System.out.println(i);
}
}
private static int getNumber() {
Random random = new Random();
return random.nextInt(43)+1;
}
}
Friend "Oh! I could write a pretty good code"
I "Ehehe : kissing_closed_eyes:"
Friend "How is it? The joy of this kind of thinking process or when the code you thought of yourself worked properly would be huge."
I said, "Yeah! I was able to do it while thinking that programming was fun."
Friend "It was good. Please continue to do your best."
I said, "Of course. By the way, what kind of program would you write if you were a friend?"
Friend "Is it me? That's right for me ..."
Loto6
public class Loto {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<Integer>();
for(int i = 1;i <= 43;i++) {
numbers.add(i);
}
Collections.shuffle(numbers);
for(int i = 0;i < 6;i++) {
System.out.println(numbers.get(i));
}
}
}
Friend "Well, I wonder if I can use the List class and the Collections class to get out quickly"
** I "Ahokusa" **
Programming fun!