无法根据Hashmap中的键打印值

时间:2016-12-18 23:33:40

标签: java if-statement hashmap user-input do-while

我无法在我的代码中使选项7工作(即选择== 7),即输出学生的测验分数。 当我运行程序时,它只是要求跳转到原始选择集。 任何帮助将非常感激。

package studentquizgrades;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;


public class StudentQuizGrades {

public static void main(String[] args) {
    Map<String, Student> map = new HashMap<>();

    addStudent(map);

}

private static void addStudent(Map<String, Student> map) {
    Scanner userInput = new Scanner(System.in);

    boolean finish = false;


    do {
        System.out.println("Please choose an option: ");
        System.out.println("Add student and quizzes - 1, Get all quiz scores - 2, Get highest quiz score- 3, ");
        System.out.println("Get lowest quiz score - 4, Get class average - 5, View a list of all students - 6");
        System.out.println("Get a student's quiz scores - 7, Quit - 8");
        int choice = userInput.nextInt();

        if (choice == 1) {
            Set<String> keySet = map.keySet();
            System.out.println("How many students would you like to add?");
            int numberOfStudents = userInput.nextInt();
            for (int counter = 0; counter < numberOfStudents; counter++) {

                System.out.println("ENTER NAME");
                Scanner addName = new Scanner(System.in);
                String name = (addName.nextLine());

                System.out.println("Enter First Quiz Score");
                Scanner addQuiz1 = new Scanner(System.in);
                int quiz1 = (addQuiz1.nextInt());

                System.out.println("Enter Second Quiz Score");
                Scanner addQuiz2 = new Scanner(System.in);
                int quiz2 = (addQuiz2.nextInt());

                System.out.println("Enter Third Quiz Score");
                Scanner addQuiz3 = new Scanner(System.in);
                int quiz3 = (addQuiz3.nextInt());
                Student student = new Student(name, quiz1, quiz2, quiz3);
                map.put(student.getKey(), student);

            }

        } else if (choice == 2) {
            Set<String> keySet = map.keySet();
            for (String currentKey : keySet) {
                Student student = map.get(currentKey);
                System.out.println();
                System.out.println(currentKey);
                System.out.println(Arrays.toString(student.getQuizGrades()));
                System.out.println();

            }

        } else if (choice == 3) {
            Set<String> keySet = map.keySet();
            int max = 0;
            String maxName = null;

            for (String currentKey : keySet) {

                Student student = map.get(currentKey);
                int[] scores = student.getQuizGrades();

                for (int counter = 1; counter < scores.length; counter++) {
                    if (scores[counter] > max) {
                        max = scores[counter];
                        maxName = currentKey;
                    }
                }
            }
            System.out.println();
            System.out.println("The highest quiz score was " + max + "; his/her name is " + maxName);
            System.out.println();

        } else if (choice == 4) {
            Set<String> keySet = map.keySet();
            int min = Integer.MAX_VALUE;
            String minName = null;

            for (String currentKey : keySet) {
                Student student = map.get(currentKey);
                int index = 0;
                int[] scores = student.getQuizGrades();

                for (int counter = 0; counter < scores.length; counter++) {
                    if (scores[counter] < min) {
                        minName = currentKey;
                        min = scores[counter];

                    }

                }

            }
            System.out.println();
            System.out.println("The lowest quiz score was " + min + "; his or her name is " + minName);
            System.out.println();

        } else if (choice == 5) {
            Set<String> keySet = map.keySet();
            int[] allGrades;
            int sum = 0;
            int counter2 = 0;
            for (String currentKey : keySet) {
                Student student = map.get(currentKey);
                int[] scores = student.getQuizGrades();
                for (int counter = 0; counter < scores.length; counter++) {
                    int j = scores[counter];
                    sum = sum + j;
                    counter2++;
                }

            }
            int average = sum / counter2;
            System.out.println("");
            System.out.println("The class average is: " + average);
            System.out.println("");
        } else if (choice == 6) {
            Set<String> keySet = map.keySet();
            System.out.println("");
            System.out.println("List of students: ");
            for (String currentKey : keySet) {
                Student student = map.get(currentKey);

                System.out.println(currentKey);

            }
        } 
        else if(choice == 7){


            Set<String> keySet = map.keySet();
            System.out.println("");
            System.out.println("Please enter a student's name: ");
            String studentName = userInput.nextLine();
            for (String currentKey : keySet) {

                Student student = map.get(currentKey);

                if(studentName == currentKey){

                    System.out.println(currentKey + "'s quiz scores:");
                    int [] quizArray = student.getQuizGrades();
                    for(int counter1 = 0; counter1 < quizArray.length; counter1++ ){

                        System.out.println(quizArray[counter1]);

                    }
                }

            }
        }


        else if (choice == 8) {
            finish = true;
            break;
        }
    } while (finish == false);
}
}

package studentquizgrades;

public class Student {
private String key;
private int grade1;
private int grade2;
private int grade3;

    public Student(String key, int grade1, int grade2, int grade3){
        this.key = key;
        this.grade1 = grade1;
        this.grade2 = grade2;
        this.grade3 = grade3;
    }

    public String getKey(){
        return key;
    }

    public int[] getQuizGrades(){
       int [] anArray = {grade1, grade2, grade3};
       return anArray;
    }

    public int getAverageScore(){
        int average = (grade1 + grade2 + grade3)/3;
        return average;
    }

}

2 个答案:

答案 0 :(得分:3)

您混合scanner.nextInt()scanner.nextLine()。 当您致电scanner.nextInt()然后致电scanner.nextLine()时,scanner.nextLine()会返回scanner.nextInt()输入的当前输入行的剩余部分,即空字符串。在你输入任何东西之前 这就是你循环到原始选择的原因。

您应该避免链接next()nextLine()。尽量只使用一个或另一个。

此外,else if(choice == 7)块中的代码将字符串值与==进行比较,此外甚至不需要进行此比较,因为您在学生地图上执行循环以检索学生可以直接检索信息的地图:

Set<String> keySet = map.keySet();
System.out.println("");
System.out.println("Please enter a student's name: ");
String studentName = userInput.next();
for (String currentKey : keySet) {

     Student student = map.get(currentKey);

     if(studentName == currentKey){

             System.out.println(currentKey + "'s quiz scores:");
             int [] quizArray = student.getQuizGrades();
              for(int counter1 = 0; counter1 < quizArray.length; counter1++ ){
                      System.out.println(quizArray[counter1]);
               }
      }

}

你可以做到:

System.out.println("");
System.out.println("Please enter a student's name: ");
String studentName = userInput.next();

Student student = map.get(studentName);
if (student!=null){
      System.out.println(studentName + "'s quiz scores:");
      int[] quizArray = student.getQuizGrades();
      for (int counter1 = 0; counter1 < quizArray.length; counter1++) {
         System.out.println(quizArray[counter1]);
      }
 }

答案 1 :(得分:2)

而不是if (studentName == currentKey)使用if (studentName.equals(currentKey)。您应始终将字符串与equals(...)进行比较,因为==仅检查引用的相等性,而不是对象本身。由于studentNamecurrent键是两个独立的实例,因此它们在==的comaprison中永远不会相同,因为它们的引用不同。

您应该尝试重构代码,因为存在多个问题。正如@markspace已经指出的那样,你的循环是不必要的,根本不需要重要的if语句。

for (String currentKey : keySet) {
    Student student = map.get(currentKey);
    if(studentName == currentKey){
        //...
    }
}

在处理地图时,可以将其缩减为以下代码。这是地图的重点,不必迭代所有元素,而是使用键直接访问元素。

Student student = map.get(currentKey);
if (student != null) {
    // ...
}

此外,您应该在nextLine(); (and other next...(...); calls)之后致电int choice = userInput.nextInt();,因为他们不会使用换行符。否则studentName = userInput.nextLine();将仅包含上次调用nextInt()...;时剩余的换行符。