ERROR: This method must return a result of type int

前端 未结 5 1650
耶瑟儿~
耶瑟儿~ 2021-01-25 03:51

Code:

import java.util.*;

public class shuffleDeck
{ 
    public static int shuffleDeck (int[] deck, int theNumber)
    {
        int [] array1         


        
5条回答
  •  礼貌的吻别
    2021-01-25 04:22

    On java, when you define a method, that method must either must return a value, or must be declared with the void keyword.

    public static int shuffleDeck(int[] deck);
    

    means, you are going to return a primitive integer (int) with the use of return keyword.

    public static int shuffleDeck(int[] deck);
    

    means, you are not going to return something, thus void is used here to declare that.

    Finally, I think this is what you try to accomplish, there were some several problems on the code you have provided, may be you can go over the sample below;

    import java.util.Random;
    
    public class Test1 {
    
        public static void shuffleDeck(int[] deck) {
            int[] array1 = new int[52];
    
            Random random = new Random();
            for (int i = deck.length, j, tmp; i > 1; i--) {
                j = random.nextInt(i);
    
                tmp = deck[i - 1];
                deck[i - 1] = deck[j];
                deck[j] = tmp;
            }
        }
    
        public static void main(String[] args) {
            int[] deck = new int[52];
            for (int i = 0; i < deck.length; i++) {
                deck[i] = i + 1;
            }
    
            System.out.println("Initial Ordered Deck");
            printDeck(deck);
    
            int count;
            count = 1;
            int total = 1;
    
            shuffleDeck(deck);
    
            System.out.println("Shuffled Deck");
            printDeck(deck);
    
        }
    
        private static void printDeck(int[] deck) {
            System.out.println("**************************************");
    
            for (int i = 0; i < deck.length; i++) {
                if (i % 13 == 0 && i > 0 )
                    System.out.println();
    
                System.out.printf("%2d ", deck[i]);
            }
    
            System.out.println("\n**************************************");
            System.out.println();
        }
    
    }
    

    And the output is;

    Initial Ordered Deck
    **************************************
     1  2  3  4  5  6  7  8  9 10 11 12 13 
    14 15 16 17 18 19 20 21 22 23 24 25 26 
    27 28 29 30 31 32 33 34 35 36 37 38 39 
    40 41 42 43 44 45 46 47 48 49 50 51 52 
    **************************************
    
    Shuffled Deck
    **************************************
    22  6 13 11 35 23 29 27  8 30 44 20  1 
    31 34 28 47  5 46 17 51 38  3 19 36 18 
    42 33  7  4  2 24 41  9 15 45 21 16 37 
    14 48 43 49 32 12 40 39 26 50 52 10 25 
    **************************************
    

提交回复
热议问题