有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java扫描器输入到int和string数组中

如果有人愿意帮助我使用这个程序,我们将不胜感激,它使用扫描仪接受多个学生的名字和成绩,然后将它们放入两个数组,学生和分数。然后它会像下面那样打印出来

最高等级=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) 个答案

  1. # 1 楼答案

    我会使用最小值和最大值作为常数

    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. # 2 楼答案

    您正在将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] );
    
  3. # 3 楼答案

    您正在用不正确的值比较最小值和最大值。学生是指你没有成绩的学生人数。此外,当打印tha名称时,您打印的是整个数组,而不仅仅是一个特定的值。所以我的建议是创建两个变量,如下所示:

    int minInd = 0; 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];

    然后像这样打印结果:

    System.out.println("Min. Grade = " + min + " ("+ name[minInd]+")"); System.out.println("Max. Grade = " + max + " ("+name[maxInd]+")");