选4张卡片作业

时间:2015-10-20 02:25:19

标签: java

注意:这已经解决了。我在后面的帖子中发布了下面的工作代码。

首先,我知道这里有一个类似的问题:Pick four cards and compute their sum JAVA 然而,他们的剧本的结果与我们需要的不同,他们只是计算4张随机牌。我需要找到存在的4张卡的组合。

我目前在我的第一个Java编程课程中。我们已经涵盖了方法和数组,但还没有关于类和对象的内容。因此,如果您选择回答,请记住这一点。

本周我的作业是编写一个程序,在52个牌组中找到4张牌的每个可能组合,加起来为24.(Ace为1,Jack 11,Queen 12和King 13)我已发布我知道下面的代码有一些错误,它不适合我想要它。我在这里张贴,看看我是否走在正确的轨道上。我的导师说正确的答案是12,517,我们应该拿出答案。任何提示都将不胜感激。

要求的具体问题 - “如何更改下面的代码,产生12,517的输出”

我知道的事情:

  1. 我知道迭代中缺少一些数字,第4个堆栈重置为4而不是1。我还没弄明白如何纠正这个问题。

  2. 我知道我最深的For循环会在继续之前循环相同的组合4次...我不知道为什么(或如何)这样做。

  3. 注意!:我在“计算”方法中输出消息以进行调试。如果你想使用它们,请先启动然后立即停止脚本,这会给你一个想法。如果希望程序运行直到完成,则在嵌套的4循环中注释掉3个输出消息。

        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);
    
    
    
    
    
        }
    
    
    
     }
    

2 个答案:

答案 0 :(得分:0)

我会这样做:

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
}

如果我可以澄清你的问题:你的意思并不是要求每一个&#34;组合&#34;卡(所以打印出所有12,517种可能性)。

相反,您的意思是获得由计数器表示的组合总数。

我的四个for循环正在做的非常简单:它使用Ace作为1和King作为13来完成所有可能性。如果四张牌的总和等于(==)24,则将一个加到计数器。

由于嵌套for循环的性质,这将通过所有四组13C1组合子。

我希望这有帮助!

注意:如果您不了解:在带括号(Java,C)的语言中,如果您使用条件语句或循环(if / else,while,for)只有一个以下语句,就像在我的代码中一样,你可以省略括号。

答案 1 :(得分:0)

这是生成正确结果的工作代码。关键是将子堆栈父级父堆栈+ 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);

    }


}