Java Scanner输入到int和string数组中

时间:2015-11-13 18:06:22

标签: java arrays string int

如果有人愿意帮助我完成这个课程,我们将不胜感激,它会使用扫描仪接受多个学生的姓名和成绩,然后将它们分成2个阵列,即学生和分数。然后它将打印出如下......

最大。等级= 98(劳伦)

最小。等级= 50(乔)

平均。等级= 83.9

/* Chris Brocato
 *  10-27-15
 * This program will read the students names and scores using a Scanner and use two arrays to 
 * show the grade and name of the highest and lowest scoring student as well as the average grade.*/

import java.util.*;

public class StudentCenter {

    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        System.out.print("Please enter the number of students: ");
        int students = console.nextInt();
        String[] name = new String[students];
        int[] scores = new int[students];

        int min = 0; int max = 0; int sum = 0;
        for (int i = 0; i < name.length; i++) {
            System.out.print("Please enter student's name: ");
            name[i] = console.next();
            System.out.print("Now enter their score: ");
            scores[i] = console.nextInt();
            if (i == 0) {
                min = students;
                max = students;
            }else {
                if (students < min) min = students;
                if (students > max) max = students;
            }sum += students;
        }
        System.out.println("Min. Grade = " + min + name );
        System.out.println("Max. Grade = " + max + name);
        System.out.println("Average Grade = " + sum);
        double avg = (double) sum / (double) students;
        System.out.println("Avg = " + avg);
        console.close();
        }   

    }

3 个答案:

答案 0 :(得分:1)

您将minmaxsum设置为students的值,即学生的数量 - 而不是他们的分数。您应该将它们设置为scores[i]

if (i == 0) {
    min = scores[i];
    max = scores[i];
}else {
    if (students < min) min = scores[i];
    if (students > max) max = scores[i];
}
sum += scores[i];

我还会将指数存储为最低和最高分数,以便您稍后可以引用它们的名称。

 min = scores[i];
 minIndex = i;
 ...
 System.out.println("Min. Grade = " + min + name[minIndex] );

答案 1 :(得分:0)

我会使用常量的最小值和最大值。

int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
int maxValue = 0;
int minValue = 0;
String minName;
String maxName;

//then use them for comparison in the loop

if(scores[i] < min)
{
 minValue = scores[i];
 minName = name[i];
}

if(scores[i] > max)
{
 maxValue = scores[i];
 maxName = name[i];
}

这将使用相关名称存储您的最大/最小值。

答案 2 :(得分:0)

您正在将min和max与不正确的值进行比较。学生是你没有成绩的学生人数。此外,在打印名称时,您将打印整个阵列而不仅仅是特定值。所以我的建议是你创建两个这样的变量:

Array ( [0] => Array ( [0] => 01000 [SQLSTATE] => 01000 [1] => 0 [code] => 0 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]0510620150922777 [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]0510620150922777 ) [1] => Array ( [0] => 01000 [SQLSTATE] => 01000 [1] => 0 [code] => 0 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]UPDATE 004 [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]UPDATE 004 ) ) int minInd = 0;

然后改变你的if:

int maxInd = 0;

打印结果如下:

if (i == 0) { min = scores[i]; max = scores[i]; } else { if (scores[i] < min) { min = scores[i]; minInd = i; } if (scores[i] > max) { max = scores[i]; maxInd = i; } } sum += scores[i];