【问题标题】:I am trying to do a grade array with random #'s and converting to letter grades我正在尝试使用随机#做一个等级数组并转换为字母等级
【发布时间】:2016-10-16 18:00:06
【问题描述】:
 */
public static double ranNumber(double [] gradeArray) {
    double rN = 0;
    for (int i = 0; i < gradeArray.length; i++) {
        rN = 55 + Math.random() * (100 - 55) + 1;    //this section is to generate a # between 55-100. 
     }

    return rN;
}

  /**
 *
 * @param input
 * 
 */
public static void outputArray(double [] input) {  // this is where I want the random # to go and to later convert to letter grade. 

    for (int count = 1; count <= input.length; count++) {
        System.out.printf("Grade %d: %.2f is a  ", count+1 , input[count]);
    }
}

好的,所以我只发布了这两种方法,因为我想要尝试做的是将随机数放入 outputArray 以便我可以将双精度转换为字母等级。我的输出应该类似于“1 年级:93.23 是 A-”。

【问题讨论】:

  • 您对返回字母等级的方法的尝试在哪里?
  • 或者你在哪里打电话给ranNumber?为什么会生成gradeArray.length - 很多数字,但只返回一个值?

标签: java arrays random


【解决方案1】:

据我所知,您想要生成一组随机成绩。您的原始函数不需要传入数组参数,它应该返回一个。

public static double[] ranNumber(int len) {
    double[] rN = new double[len];
    for (int i = 0; i < len; i++) {
        rN[i] = 55 + Math.random() * (100 - 55) + 1;    //this section is to generate a # between 55-100. 
     }

    return rN;
}

然后,在outputArray 中调用该方法并循环它。或者作为参数从main传递过来

警告...int count = 1; count &lt;= input.length...input[count] 会抛出异常

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多