0

私は数字を計算するためにこのコードを作りました:

import java.util.Random;

public class Sorting {
private double[] player;
private int k=5;
private int j=5;

public void sort(){
    player = new double[k];
    for(int i=1;i<k;i++){
        double tempp ;
            for(i=1;i<j;i++){
                tempp = Math.random() * i;
                player[i]=tempp;
                System.out.println("Result "+i+"="+player[i]);
            }
        }
} 

public static void main(String []args){
    Sorting k=new Sorting();
    k.sort();
}}

結果は次のとおりです。

Result 1=0.4529689730194949
Result 2=0.09643822768644617
Result 3=1.841047494651026
Result 4=2.1807153629323777

ここで、次のように、EXCELLENT、VERY GOOD、GOOD、BADというラベルの付いた結果番号の大きいものから小さいものへのラベルを追加します。

Result 1=0.4529689730194949 labeled GOOD
Result 2=0.09643822768644617 labeled BAD
Result 3=1.841047494651026 labeled VERY GOOD
Result 4=2.1807153629323777 labeled EXCELLENT
4

2 に答える 2

0
public class Sorting
{
    private double[] player;
    private int k=5;
    private int j=5;

    String[] rating = {"BAD", "GOOD", "VERY GOOD", "EXCELENT"};

    public void sort()
    {
        player = new double[k];

        for(int i=1; i<k; i++)
        {
            double tempp;
            for(i=1; i<j; i++) // i should probably be zero since your array starts on value 0, not 1.
            {
                tempp = Math.random() * i;
                player[i]=tempp;
                System.out.println("Result " + i + " = " + player[i] + " Rating is " + rating[(int)player[i]]);
            }
        }
    } 

    public static void main(String[] args)
    {
        Sorting k = new Sorting();
        k.sort();
    }
}

あなたのコードは少し奇妙に思えますが、あなたが求めていた問題を修正しました。

于 2012-09-29T23:20:18.790 に答える
0

TreeSet は、デフォルトで数値を昇順に並べ替えます。

ここに私のバージョン:

出力例:

結果: 0.12754837127918317 => 悪い
結果: 0.7956890627771006 =>
優れた結果: 0.3123868511945034 => 良い
結果: 0.6332109887264882 => VERY_GOOD

public class FourRandomNumbersEvaluator {

    private TreeSet<Double> numbers;

    private Map<Double,Evaluation> numbersWithEvaluations = new HashMap<Double,Evaluation>();

    private enum Evaluation {BAD, GOOD, VERY_GOOD, EXCELLENT}

    private static final int NUMBER_OF_GENERATED_NUMBERS = 4;


    public FourRandomNumbersEvaluator(TreeSet<Double> numbers) {
        if(numbers == null || numbers.size() != 4){
            throw new IllegalArgumentException("your have to provide exactly 4 numbers");
        }
        this.numbers = numbers;
    }

    public static void main(String[] args) {
        FourRandomNumbersEvaluator evaluator = new FourRandomNumbersEvaluator(generateNumbers());
        evaluator.evaluate();
        evaluator.printNumbersWithEvaluations();
    }

    private static TreeSet<Double> generateNumbers() {
        TreeSet<Double> numbers = new TreeSet<Double>();
        while(numbers.size() < NUMBER_OF_GENERATED_NUMBERS){
            double number = Math.random();
            if(numberNotAlreadyExisting(numbers, number)){
                numbers.add(number);
            }
        }
        return numbers;
    }

    private static boolean numberNotAlreadyExisting(TreeSet<Double> numbers, double number) {
        return !numbers.contains(number);
    }

    public void evaluate() {
        int i = 0;
        for(Double number : numbers){
            numbersWithEvaluations.put(number, Evaluation.values()[i++]);
        }
    }

    private void printNumbersWithEvaluations(){
        for(Map.Entry<Double,Evaluation> numberWithEvaluation : numbersWithEvaluations.entrySet())
        System.out.println("Result: "+  numberWithEvaluation.getKey() + " => " + numberWithEvaluation.getValue());
    }
}
于 2012-09-29T23:51:38.557 に答える