如何获取数组并将所有数字转换为其他数字。 'JAVA'

时间:2015-03-13 00:36:56

标签: java arrays for-loop

我正在尝试用java制作GPA计算器。一旦我拥有了我的数组中的所有成绩,我希望有另一个数组将所有GPA放入相同大小的数组中。

例如arrayA = [90,95,80,87,100] ....

我希望能够将这些数字转换为arrayB并使其相等[3.7,4.0,2.7,3.3,4.3]

这是我到目前为止所做的:

import java.util.Scanner;
import java.util.Arrays;
/**
 Coleman Strauss
 GPA Calculator
 March 12, 2015
 */
public class GPACalculator
{
    public static void main (String[] args) 
    {
        int numClasses;
        Scanner scan = new Scanner(System.in);
        double[] myArrayGrades;
        double[] myArrayGPA;


        System.out.println("Please enter in the amount of classes you have:   ");
        numClasses = scan.nextInt();

        System.out.println("Alright so you have " + numClasses + " classes.");

        myArrayGrades = new double[numClasses];

        for (int i=0; i < numClasses; i++) {
            System.out.println("Please enter all your grades in a row!");

        }
        System.out.println(myArrayGrades);
    }
}

3 个答案:

答案 0 :(得分:1)

如果你有像

这样的地图
Map<Double, Double> gpas = new HashMap<>();
gpas(100d, 4d);
gpas(90d, 3.5);

然后你可以循环遍历原始数组并从地图中检索相应的gpa并填满第二个数组。

int i = 0;
for(Integer grade : myArrayGrades){
myArrayGPA[i++] = map.get(grade);
}

这可能会对你有帮助。

答案 1 :(得分:0)

这很简单,真的。测试尺寸是否相同,如果是,则转换。别忘了初始化数组

if(arrayA.size() == arrayB.size()){
//do nothing
}
else{
exit; //array sizes are mismatched and will cause OutOfBounds exception
}

for(int i = 0; i < arrayA.size(); i++){
arrayB[i] = arrayA[i]*GPAConditional; /*GPAConditional is the constant conversion factor used to convert grades into GPA, ultimately defined by you*/ 
}

请注意,对arrayA&#39; s元素的更改会改变arrayB的元素!!

答案 2 :(得分:0)

我找到了一种方法来获取我需要的所有值到数组中。我没有使用两个数组,而是立即将等级转换为GPA并将它们存储到数组中,如下所示:

    import java.util.Scanner;
    import java.util.Arrays;
    /**
     Coleman Strauss
     GPA Calculator
     March 12, 2015
     */
    public class GPACalculator
    {
        public static void main (String[] args) 
        {
            int numClasses;
            Scanner scan = new Scanner(System.in);
            double[] myArrayGrades;
            double sumGPA = 0;


    System.out.println("Please enter in the amount of classes you have: ");
    numClasses = scan.nextInt();

    System.out.println("Alright so you have " + numClasses + " classes.");

    myArrayGrades = new double[numClasses];


    for (int i=0; i < numClasses; i++) {
        System.out.println("Please enter all your grades in a row!");
        myArrayGrades[i] = scan.nextDouble();

        if (myArrayGrades[i] >= 97) {
            myArrayGrades[i] = 4.3; 
        }
        else if (myArrayGrades[i] >= 93) {
            myArrayGrades[i] = 4.0;
        }
        else if (myArrayGrades[i] >= 90) {
            myArrayGrades[i] = 3.7;
        }
        else if (myArrayGrades[i] >= 87) {
            myArrayGrades[i] = 3.3;
        }
        else if (myArrayGrades[i] >= 83) {
            myArrayGrades[i] = 3.0;
        }
        else if (myArrayGrades[i] >= 80) {
            myArrayGrades[i] = 2.7;
        }
        else if (myArrayGrades[i] >= 77) {
            myArrayGrades[i] = 2.3;
        }
         else if (myArrayGrades[i] >= 73) {
            myArrayGrades[i] = 2.0;
        }
        else if (myArrayGrades[i] >= 70) {
            myArrayGrades[i] = 1.7;
        }
        else if (myArrayGrades[i] >= 67) {
            myArrayGrades[i] = 1.3;
        }
        else if (myArrayGrades[i] >= 63) {
            myArrayGrades[i] = 1.0;
        }
        else if (myArrayGrades[i] >= 60) {
            myArrayGrades[i] = .07;
        }
        else {
            myArrayGrades[i] = 0.0;
        }
    }

    System.out.println("Here are all of your GPA's for each class");
    System.out.println(Arrays.toString(myArrayGrades));

    for (int i = 0; i <= numClasses; i ++) {
        myArrayGrades[i] += sumGPA;
    }

    System.out.println("The sum of the GPA's is: " + sumGPA);

} }