错误:此方法必须返回int类型的结果 [英] ERROR: This method must return a result of type int
问题描述
代码:
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();
}
}
方法shuffleDeck出错。我不确定这意味着什么我需要返回一些东西,但它不会返回,我得到这个奇怪的错误。
There is an error on the method 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.
推荐答案
在java上,当你定义一个方法时,那个方法必须要么必须返回一个值,要么必须用void关键字声明。
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);
表示您将返回一个原始整数( int )使用 返回 关键字。
means, you are going to return a primitive integer (int) with the use of return keyword.
public static int shuffleDeck(int[] deck);
表示您不会返回某些内容,因此 void 来声明。
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();
}
}
输出为;
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
**************************************
这篇关于错误:此方法必须返回int类型的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!