简体   繁体   English

错误:此方法必须返回int类型的结果

[英]ERROR: This method must return a result of type int

Code: 码:

import java.util.*;

public class shuffleDeck
{ 
    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;
        }
    }

    public static void main(String[] args)
    {
        int [] deck = new int [52];
        for(int i=0; i<52; i++)
        {
            deck[i]=i+1;
        }
        int count;
        count=1;
        int total=1;
        shuffleDeck(deck, count);
        System.out.println();
    }
}

There is an error on the method shuffleDeck. shuffleDeck方法有错误。 I am not sure what it means I need to return something but it wont return and I am getting this odd error. 我不确定这意味着什么我需要返回一些东西,但它不会返回,我得到这个奇怪的错误。

I have not been able to solve this I have looked around all over stack. 我一直无法解决这个问题。 Thank you to whoever can help me fix this error. 感谢能帮助我解决此错误的任何人。

On java, when you define a method, that method must either must return a value, or must be declared with the void keyword. 在java上,当您定义方法时,该方法必须必须返回一个值,或者必须使用void关键字声明。

public static int shuffleDeck(int[] deck);

means, you are going to return a primitive integer ( int ) with the use of return keyword. 意味着,您将使用return关键字返回原始整数( int )。

public static int shuffleDeck(int[] deck);

means, you are not going to return something, thus void is used here to declare that. 意思是,你不会返回某些东西,因此在这里使用void来声明它。

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 
**************************************

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. 如果你定义一个return type你必须提供一个。 void defines that no return type is needed. void定义不需要返回类型。 In this case remove: 在这种情况下删除:

return theNumber;

inside the shuffleDeck method. shuffleDeck方法中。

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, 因为你已经将返回类型声明为int并且你已经在for循环中给出了返回语句,现在想想如果你的代码不进入for循环会比没有return语句会发生什么,所以,

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

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 . 你的算法仍有bug

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: 根据你的主要方法,我想,你想要的是在指定的count上洗牌,因为你应该按如下方式更新方法:

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: 发布在https://stackoverflow.com/a/30757452/4234940中的内部循环本身就是shuffle ...所以实际上我会改变它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM