0

Arrays.sort() 给出和错误:

FiveDice.java:19: error: no suitable method found for sort(int)
   Arrays.sort(compNums);

如果我从 for 循环中取出任何内容,它会认为你只是 1 个数字或给出错误。还有哪些其他排序选项可用?

import java.util.*;
public class FiveDice {
    public static void main(String[] args) {
        int x;
        int compNums = 0;
        int playerNums;

        Die[] comp = new Die[5];
        Die[] player = new Die[5];
        System.out.print("The highest combination wins! \n5 of a kind, 4 of a kind, 3 of a kind, or a pair\n");
//computer
        System.out.print("Computer rolled:  ");
        for(x = 0; x < comp.length; ++x) {
            comp[x] = new Die();
            compNums = comp[x].getRoll();
           //Arrays.sort(compNums); <--does not work
           System.out.print(compNums + " ");
        }
//player
        System.out.print("\nYou rolled: \t  ");
        for(x = 0; x < player.length; ++x) {
            player[x] = new Die();
            playerNums = player[x].getRoll();
            System.out.print(playerNums + " ");
        }
    }
}

模具类

public class Die {
    int roll;
    final int HIGHEST_DIE_VALUE = 6;
    final int LOWEST_DIE_VALUE = 1;
    public Die()
       {   } 
    public int getRoll() { 
        roll = ((int)(Math.random() * 100) % HIGHEST_DIE_VALUE + LOWEST_DIE_VALUE);
        return roll; }
    public void setRoll()
        { this.roll = roll; } 

}

4

4 回答 4

1

最简单的方法是实现 Comparable to Die 类,在 Die 的构造函数中设置 roll 的值而不是在 getter 方法中,您的问题就解决了。

public class Die implements Comparable<Die> {
    private int roll;
    final int HIGHEST_DIE_VALUE = 6;
    final int LOWEST_DIE_VALUE = 1;
    public Die() {
        roll = ((int)(Math.random() * 100) % HIGHEST_DIE_VALUE + LOWEST_DIE_VALUE);
    }


    public int getRoll() {
         return roll;
    }        
    public void setRoll(int roll) {
         this.roll = roll;
    }

    public int compareTo(Die d) {
         return new Integer(d.getRoll()).compareTo(new Integer(this.getRoll()));
    }

}

现在Arrays.sort(Die[])将对 Die 的数组进行排序。

于 2013-10-19T07:41:41.710 回答
0

您不必使用 2 个数组进行排序。如果您实现该Comparable<T>接口,您的类可以按 Java Collections API 进行排序。在您的情况下,Die类可以实现Comparable<T>并为 Java 框架提供一种比较骰子值的方法。

看看 Java API: http ://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html

在你的情况下:

public class Die implements Comparable<Die>{

    int roll;
    final int HIGHEST_DIE_VALUE = 6;
    final int LOWEST_DIE_VALUE = 1;

    public Die() {   } 

    public int computeRoll() { 
        roll = ((int)(Math.random() * 100) % HIGHEST_DIE_VALUE + LOWEST_DIE_VALUE);
        return roll;
    }

    // I also changed this to store the value
    public int getRoll() { 
        return roll; 
    }
    public void setRoll() { this.roll = roll; }

    // This is the method you have to implement
    public int compareTo(Die d) {
        if(getRoll() < d.getRoll()) return -1;
        if(getRoll() > d.getRoll()) return +1;
        if(getRoll() == d.getRoll()) return 0;
    }

}

每当您有一个CollectionDies 时,例如 ArrayList 或 LinkedList,您只需对集合本身进行排序。下面是一个示例代码。

ArrayList<Die> myCollection = new ArrayList<Die>();
myCollection.add(die1);
// more array population code
// ...
Collections.sort(myCollection);
于 2013-10-19T07:38:09.933 回答
0

你必须将一个数组传递给Array.Sort(arr)not 参数你必须做这样的事情Array.Sort(int[]compNums);

如果您使用的是匿名类型comp[]compNums,则必须这样做

java.util.Arrays.sort(comp[]compNums, new java.util.Comparator<Object[]>() {
        public int compare(Object a[], Object b[]) {
            if(something)
                return 1;

                return 0;
        }
    });
于 2013-10-19T07:50:58.390 回答
0

你不能执行sort()compNums因为它是一个单一的值。您将其声明为一个int值,而不是一个整数数组。

相反,您应该创建compNums一个数组,用Die滚动值填充它,然后对结果数组执行排序操作。我相信以下将实现您所追求的。

int[] compNums = new int[5];
...
for(x = 0; x < comp.length; ++x) {
    comp[x] = new Die();
    compNums[x] = comp[x].getRoll();
}
Arrays.sort(compNums);
// print results
于 2013-10-19T07:01:42.723 回答