Picking four cards(In java) Write a program that picks four cards from a deck of
ID: 3714030 • Letter: P
Question
Picking four cards(In java)
Write a program that picks four cards from a deck of 52 cards and compute their sum. An Ace, King,
Queen, Jack represent 1,13,12,11, respectively. Your program needs to ask the user for the sum, then
find the number of the total combinations, the number of the combinations that yields the sum. And the
list of all the combinations that yields the sum.
Please follow this shell when coding
https://shrib.com/#u6f6q24n3ax4dDXNTHlY
Here is the sample output
Enter the sum of the combinations that you want:52
Here is the list of all the combinations:
13 13 13 13
Total number of combinations: 270725
The number of picks that yields the sum of 52 is 1
Do you want to continue: yes
Enter the sum of the combinations that you want: fgg
Enter the sum of the combinations that you want : dfgg
Enter the sum of the combinations that you want: dffg
Enter the sum of the combinations that you want: fdgfg
Enter the sum of the combinations that you want: -45
Enter the sum of the combinations that you want: 51
Here is the list of all the combinations:
12 13 13 13
12 13 13 13
12 13 13 13
12 13 13 13
13 12 13 13
13 12 13 13
13 12 13 13
13 13 12 13
13 13 12 13
13 13 13 12
13 13 12 13
13 12 13 13
13 13 12 13
12 13 13 13
13 12 13 13
13 13 12 13
Total number of combinations: 270725
The number of picks that yields the sum of 51 is 16
Do you want to continue: no
Explanation / Answer
Let me know if you have any doubt.
import java.util.Scanner;
public class CardsShell{
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
sum(kb);
}
public static void sum(Scanner kb){
int[] card = initialize();
int counter = 0;
int sum = 0;
int totCombination = 0;
String check = "yes";
while(check.toLowerCase().equals("yes")){
sum = dataValidation(kb);
System.out.println("Here is the list of all combinations: ");
counter = 0;
totCombination = 0;
for(int i = 0;i<13;i++){
for(int j = 13;j<26;j++){
for(int k = 26;k<39;k++){
for(int l = 39;l<52;l++){
if((card[i] + card[j] + card[k] + card[l]) == sum){
counter++;
System.out.println(card[i]+" "+card[j]+" "+card[k]+" "+card[l]);
}
totCombination++;
}
}
}
}
System.out.println("Total Number of Combination : "+totCombination);
System.out.println("The number of picks that yeild the sum of "+sum+" is "+counter);
System.out.println("Do you want to continue (yes/no)? ");
check = kb.nextLine();
}
}
public static int dataValidation(Scanner kb){
int validInput = 0;
String line;
while(true){
try{
System.out.print(" Enter the sum of the combination that you want : ");
line = kb.nextLine();
validInput = Integer.parseInt(line);
}catch(Exception e){
validInput = -1;
kb.reset();
}
if(validInput > 0 && validInput <= 52){
break;
}
}
return validInput;
}
public static int[] initialize(){
int[] my_arr = new int[52];
int j = 1;
for(int i = 1;i<=52;i++){
my_arr[i-1] = j;
j++;
if(j > 13){
j = 1;
}
}
return my_arr;
}
}