ERROR: This method must return a result of type int

前端 未结 5 1649
耶瑟儿~
耶瑟儿~ 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:18

    Based on your main method, I suppose, that what you want is to shuffle the deck for the given count of times, for that you should update the method as follows:

    public static void shuffleDeck (int[] deck, int theNumber)
    {
        Random random = new Random();
        for (int k=0; k < theNumber; k++) {
            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;
            }
        }
    }
    

    for returning the number of how many times you actually did the shuffle doesn't make sense ... based on your input. The inner loop, as posted in https://stackoverflow.com/a/30757452/4234940 is only the shuffle itself... so actually I would change it like this:

    public static void shuffleDeck (int[] deck, int theNumber)
    {
        Random random = new Random();
        for (int k=0; k < theNumber; k++) {
            shuffle(deck, random);
        }
    }
    
    private static void shuffle(int[] array, Random random){
        for (int i = array.length, j, tmp; i > 1; i--) {
            j = random.nextInt(i);
    
            tmp = array[i - 1];
            array[i - 1] = array[j];
            array[j] = tmp;
        }
    }
    
    0 讨论(0)
  • 2021-01-25 04:20

    change

    public static int shuffleDeck (int[] deck, int theNumber)
    

    to

    public static void shuffleDeck (int[] deck, int theNumber)
    

    if you define a return type you have to deliver one. void defines that no return type is needed. In this case remove:

    return theNumber;
    

    inside the shuffleDeck method.

    0 讨论(0)
  • 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 
    **************************************
    
    0 讨论(0)
  • 2021-01-25 04:25

    Uppercase the name of your class to distinguish between class and method:

    public class ShuffleDeck {
    ...
    

    Perhaps it is even better to rename the method:

    public static int doTheShuffle(int[] deck, int theNumber) {
    

    If you want the function to return something sensible put the return out of the loop:

    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;
    }
    
    return theNumber;
    

    But all of this will only make the compiler not complain about syntax errors. Your algorithm still has bugs.

    0 讨论(0)
  • 2021-01-25 04:34

    Its because you have declared return type as int and you have given return statment inside for loop now think what will happen if your code dont go in for loop than there will be no return statement so,

    so make your code like

    public static int shuffleDeck (int[] deck, int theNumber)
      {
     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;
            return theNumber;
        }
       return 0;
    }
    
    0 讨论(0)
提交回复
热议问题