使用扫描仪输入写入2D数组值

时间:2014-11-20 05:03:22

标签: java java.util.scanner multidimensional-array

我正在使用getCategoryWeights方法(在我的代码底部)我正在编写。它应该根据用户的输入更改权重列的值,但我无法弄清楚它为什么不起作用。到目前为止,这是我的计划:

package gradeprojector;

    //Done

    // Print a welcome message
    // printWelcomeMessage();
    // Get the number of gradebook categories.
    // int categories = getGradebookCategories();
    // Get the maximum number of assignments in a category.
    // int maxAssignments = getMaximumAssignments();
    // Create the 2D array to hold assignment scores
    // double[][] scoreArray = getScoreArray(categories, maxAssignments);

    //Not Done

    // Get the category weights
    // getCategoryWeights(scoreArray);
    // Get the category scores
    // getScores(scoreArray);
    // Compute the category averages
    // computeCategoryAverages(scoreArray);
    // Compute the overall average
    // double overallAverage = computeOverallAverage(scoreArray);
    // Compute the final exam scores needed to get a certain grade (just do A,B,C,D)
    // double[] targetFinalScores = getTargetFinalScores(overallAverage);
    // Print target final exam scores
    // printTargetFinalExamScoes(targetFinalScores);

import java.util.*;
public class GradeProjector {

    public static void main(String[] args) {
        // TODO code application logic here
        printWelcomeMessage();
        int categories = getGradebookCategories();
        int maxAssignments = getMaximumAssignments();
        double[][] scoreArray = getScoreArray(categories, maxAssignments);
        printScoreArray(scoreArray, maxAssignments);
        scoreArray = getCategoryWeights(scoreArray);
        printScoreArray(scoreArray, maxAssignments);
    }

    // printWelcomeMessage();
    public static void printWelcomeMessage(){
        System.out.println("Welcome!");
    }

    // Get the number of gradebook categories.
    public static int getGradebookCategories(){
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the number of gradebook categories: ");
        int categories = in.nextInt();
        return categories;
    }

    // Get the maximum number of assignments in a category.
    public static int getMaximumAssignments(){
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the maximum number of assignments: ");
        int maxAssignments = in.nextInt();
        return maxAssignments;
    }

    // Create the 2D array to hold assignment scores
    public static double[][] getScoreArray(int categories, int maxAssignments){
        double[][] scoreArray = new double [categories] [maxAssignments+2];

        for(int i = 0; i < scoreArray.length; i++){
            for(int j = 0; j < scoreArray[i].length; j++){
                scoreArray[i][j] = -999;  
            }
        }
        return scoreArray;
    }

    public static void printScoreArray(double[][] scoreArray, int maxAssignments){
        System.out.println("");
        int compensate = -1;
        for(int i = 1; i<=scoreArray[0].length;i++ ) {
            if(i == scoreArray[0].length){
                System.out.print(" Average");
            }
            else if(i == 1)
                System.out.print("   Weight ");
            }         
            else{
                System.out.print(" Grade"+(i+compensate)+" ");
            }
        }
        System.out.println();
        if(maxAssignments == 1)
            System.out.println("  ------------------------");
        else if(maxAssignments == 2)
            System.out.println("  ---------------------------------");
        else if(maxAssignments == 3)
            System.out.println("  ----------------------------------------");
        else if(maxAssignments == 4)
            System.out.println("  ------------------------------------------------");
        else if(maxAssignments == 5)
            System.out.println("  --------------------------------------------------------");
        else if(maxAssignments == 6)
            System.out.println("  ----------------------------------------------------------------");
        else if(maxAssignments == 7)
            System.out.println("  ------------------------------------------------------------------------");
        else if(maxAssignments == 8)
            System.out.println("  --------------------------------------------------------------------------------");
        else if(maxAssignments == 9)
            System.out.println("  ----------------------------------------------------------------------------------------");
        else if(maxAssignments == 10)
            System.out.println("  -------------------------------------------------------------------------------------------------");
        else if(maxAssignments == 11)
            System.out.println("  ----------------------------------------------------------------------------------------------------------");
        else if(maxAssignments >= 12)
            System.out.println("  -------------------------------------------------------------------------------------------------------------------");

        for(int i = 0; i < scoreArray.length; i++){
            for(int j = 0; j < scoreArray[i].length; j++){
                double tempDouble = scoreArray[i][j];
                if(j==0){
                    System.out.format("%1d| ",i+1);
                    System.out.print(tempDouble+"  ");
                }
                else{
                    System.out.print(tempDouble+"  ");
                }

            }
            System.out.println();
        }
    }

    // Get the category weights
    public static double[][] getCategoryWeights(double[][] scoreArray){
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the category weights: ");

        for(int i = 0; i < scoreArray.length; i++){
            for(int j = 0; j < scoreArray[i].length; j++){
                double temp = in.nextDouble();
                scoreArray[0][j] = temp;  
            }
        }
        return scoreArray;
    }
}

3 个答案:

答案 0 :(得分:1)

如果您只想更改权重列的值,那么您应该更改类似

 for(int i = 0; i < scoreArray.length; i++){
        //for(int j = 0; j < scoreArray[i].length; j++){
            double temp = in.nextDouble();
            scoreArray[i][0] = temp;  
        //}
    }

这将仅为权重列输入...

答案 1 :(得分:0)

正如mdnghtblue的评论所说,也改变它

for(int i = 0; i < scoreArray.length; i++){
     for(int j = 0; j < scoreArray[i].length; j++){
         double temp = in.nextDouble();
         scoreArray[i][j] = temp;  
     }
 }

也错过了一点{&#39; :P

if(i == scoreArray[0].length){
    System.out.print(" Average");
}
else if(i == 1) { // { was missing
   System.out.print("   Weight ");
}         
else{
   System.out.print(" Grade"+(i+compensate)+" ");
}

答案 2 :(得分:0)

我刚才发现了问题。谢谢mdnghtblue的输入。将其更改为:

scoreArray[i][j] = temp

并将嵌套的for循环更改为:

for(int j = 0; j < 1; j++){

工作方法:

public static double[][] getCategoryWeights(double[][] scoreArray){
    Scanner in = new Scanner(System.in);
    System.out.print("Enter the category weights: ");

    for(int i = 0; i < scoreArray.length; i++){
        for(int j = 0; j < 1; j++){
            double temp = in.nextDouble();
            scoreArray[i][j] = temp;  
        }
    }
    return scoreArray;
}