toString()方法

时间:2016-04-01 18:00:49

标签: java arrays eclipse random tostring

我如何使这个toString()方法使用我在下面编写的代码返回两行的旋律和节拍?我不确定如何在for循环中加入节拍:

public String toString(){
    String result = "";
    for (double[] level: melody){
        for (double item: level)
            result += "\n";
    }
    return result;

例如,两行的意思是它应该打印出这样的东西:

  

157.18 977.21 3051.07 1887.60 1339.81 182.63 984.57 2324.46
  1.4 1.2 2.0 2.0 1.0 2.2 1.8 1.5

2 个答案:

答案 0 :(得分:0)

以下是获取范围内Random加倍的代码:

double rnd = new Random().nextDouble();
double result = 27.5 + (rnd * (4186.0 - 27.5));
System.out.println(result);

或者在方法中:

public static double nextRandomInRange(double min, double max) {
    return min + (new Random().nextDouble() * (max - min));
}

答案 1 :(得分:0)

这是代码

Random r = new Random();
double n = 27.5 + (4186 - 27.5) * r.nextDouble();
n = (double)Math.round(n * 100) / 100;