Java 选4张卡片做家庭作业
注:此问题已解决。我在后面的帖子中发布了下面的工作代码 首先,我知道这里有一个类似的问题: 然而,他们的脚本的结果与我需要的不同,他们只是计算4张随机卡。我需要找到存在的4张卡的每一个组合 我目前正在上我的第一堂Java编程课。我们已经讨论了方法和数组,但还没有讨论类和对象。所以,如果你选择回答,请记住这一点 我这周的作业是写一个程序,在52张卡片中找出4张卡片的每一个可能组合,加起来就是24张。(Ace是1,Jack 11,Queen 12,King 13)我已经发布了我的代码,我知道下面有一些错误,它不符合我的要求。我在这里发帖是为了看看我是否在正确的轨道上。我的指导老师说正确答案是12517,这取决于我们能否找到答案。如有任何提示,将不胜感激 根据要求提出的具体问题-“如何更改以下代码以生成12517的输出” 我知道的事情:Java 选4张卡片做家庭作业,java,Java,注:此问题已解决。我在后面的帖子中发布了下面的工作代码 首先,我知道这里有一个类似的问题: 然而,他们的脚本的结果与我需要的不同,他们只是计算4张随机卡。我需要找到存在的4张卡的每一个组合 我目前正在上我的第一堂Java编程课。我们已经讨论了方法和数组,但还没有讨论类和对象。所以,如果你选择回答,请记住这一点 我这周的作业是写一个程序,在52张卡片中找出4张卡片的每一个可能组合,加起来就是24张。(Ace是1,Jack 11,Queen 12,King 13)我已经发布了我的代码,我知道下面有一
public static void main(String[] args) {
int[] deck = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int total;
total = calculate(deck);
output(total);
}
public static int calculate(int[] deck){
int total = 0;
int stack1, stack2, stack3, stack4, accumulate;
for (stack1 = 0; stack1 < 52; stack1++){
for (stack2 = 1; stack2 < 52; stack2++){
for (stack3 = 2; stack3 < 52; stack3++){
for (stack4 = 3; stack4 < 52; stack4++){
accumulate = (deck[stack1] + deck[stack2] + deck[stack3] + deck[stack4]);
System.out.println(deck[stack1] + " + " + deck[stack2] + " + " + deck[stack3] + " + " + deck[stack4]);
if (accumulate == 24){
System.out.println(deck[stack1] + " + " + deck[stack2] + " + " + deck[stack3] + " + " + deck[stack4]);
total++;
System.out.println("Accumulate is at " + accumulate);
System.out.println("Currently at " + total);
}
}
}
}
}
return total;
}
public static void output(int total){
System.out.println ("The total number of card combinations of 4 that \n"
+ "equal 24 is: " + total);
}
}
publicstaticvoidmain(字符串[]args){
int[]deck={1,2,3,4,5,6,7,8,9,10,11,12,13,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
整数合计;
总计=计算(甲板);
产出(总数);
}
公共静态整数计算(整数[]甲板){
int-total=0;
int stack1,stack2,stack3,stack4,累加;
用于(stack1=0;stack1<52;stack1++){
用于(stack2=1;stack2<52;stack2++){
用于(stack3=2;stack3<52;stack3++){
用于(stack4=3;stack4<52;stack4++){
累计=(甲板[stack1]+甲板[stack2]+甲板[stack3]+甲板[stack4]);
System.out.println(deck[stack1]+“+”+deck[stack2]+“+”+deck[stack3]+“+”+deck[stack4]);
如果(累计==24){
System.out.println(deck[stack1]+“+”+deck[stack2]+“+”+deck[stack3]+“+”+deck[stack4]);
总计++;
System.out.println(“累计在”+累计);
System.out.println(“当前为”+总计);
}
}
}
}
}
返回总数;
}
公共静态无效输出(整数总计){
System.out.println(“4张卡的组合总数\n”
+“24等于:”+总计);
}
}
我会这样做:
public static void main(String[] args) {
int counter = 0; //can also just say int counter; ==> auto intialize to 0
int d, c, h, s; //diamond, club, heart, spade
for(d = 1; d < 14; d++) //each suit starts at Ace, or the value of 1
for(c = 1; c < 14; c++) //each suit ends at 13, or King
for(h = 1; h < 14; h++)
for(s = 1; s < 14; s++)
if( d + c + h + s == 24 )
counter++;
System.out.println(counter); //Your total should be your instructor's 12,517
}
publicstaticvoidmain(字符串[]args){
int counter=0;//也可以说int counter;==>自动初始化为0
int d,c,h,s;//钻石,梅花,心,黑桃
对于(d=1;d<14;d++)//每件衣服从Ace开始,或从值1开始
对于(c=1;c<14;c++)//每件衣服在13号结束,或者是国王
对于(h=1;h<14;h++)
对于(s=1;s<14;s++)
如果(d+c+h+s==24)
计数器++;
System.out.println(计数器);//您的总数应该是讲师的12517
}
请允许我澄清你的问题:你不是要问每一张卡片的“组合”(所以打印出所有12517种可能性)
相反,您的意思是获得计数器表示的组合总数
我的四个for循环所做的非常简单:它使用Ace作为1,King作为13,遍历所有的可能性。如果四张卡的总和等于(=)24,则向计数器中添加一张卡
由于嵌套for循环的性质,这将起作用,通过所有四组13C1组合运算
我希望这有帮助
注意:如果您不知道:在带括号的语言(Java,C)中,如果您使用的条件语句或循环(if/else,while,for)只包含以下一条语句,就像在我的代码中一样,您可以省略括号。以下是产生正确结果的工作代码。关键是将子堆栈设置为父堆栈+1的父堆栈:
public static void main(String[] args) {
int[] deck = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int total;
total = calculate(deck);
output(total);
}
public static int calculate(int[] deck){
int total = 0;
int stack1, stack2, stack3, stack4, accumulate;
for (stack1 = 0; stack1 < 52; stack1++){
for (stack2 = (stack1 + 1); stack2 < 52; stack2++){
for (stack3 = (stack2 + 1); stack3 < 52; stack3++){
for (stack4 = (stack3 + 1); stack4 < 52; stack4++){
accumulate = (deck[stack1] + deck[stack2] + deck[stack3] + deck[stack4]);
if (accumulate == 24)
total++;
}
}
}
}
return total;
}
public static void output(int total){
System.out.println ("The total number of card combinations of 4 that \n"
+ "equal 24 is: " + total);
}
}
publicstaticvoidmain(字符串[]args){
int[]deck={1,2,3,4,5,6,7,8,9,10,11,12,13,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
整数合计;
总计=计算(甲板);
产出(总数);
}
公共静态整数计算(整数[]甲板){
int-total=0;
int stack1,stack2,stack3,stack4,累加;
用于(stack1=0;stack1<52;stack1++){
对于(stack2=(stack1+1);stack2<52;stack2++){
对于(stack3=(stack2+1);stack3<52;stack3++){
对于(stack4=(stack3+1);stack4<52;stack4++){
累计=(甲板[stack1]+甲板[stack2]+甲板[stack3]+甲板[stack4]);
如果(累计==24)
总计++;
}
}
}
}
返回总数;
}
公共静态无效输出(整数总计){
System.out.println(“4的卡组合的总数,\n