扫描仪输入的Java Homework问题

时间:2016-09-22 00:37:05

标签: java

这是教授的提示 : 使用名为run with的方法编写名为Find2Max的公共类 以下标题:

public void run()

方法运行可以解决以下问题:

  1. 提示用户输入学生人数和

  2. 每个学生的
  3. 提示用户输入姓名和分数

  4. 完成所有学生的阅读数据后,显示两名学生 最高分。

  5. (注意,如果学生人数少于2,则该计划 还读两个学生)

    继承我的代码:

    import java.util.Scanner;
    
    public class Find2Max {
    
        public void run() { 
    
                Scanner sc = new Scanner(System.in);
                System.out.print("Enter the number of at least 2 students: ");
                int number_of_students = sc.nextInt();
    
                if(number_of_students < 2){             
                        number_of_students = 2; 
                }
    
                do{
    
                        double top_grade = 0;  
                        double second_top_grade = 0; 
                        String top_kid = ""; 
                        String second_top_kid= ""; 
    
                        for(int ii = 0; ii < number_of_students; ii++){ 
                                System.out.print("Enter a student name: ");
                                String student_name = sc.nextLine(); 
    
                                System.out.print("Enter a student score: "); 
                                Double student_grade = sc.nextDouble(); 
    
                                if(student_grade > top_grade){
                                        top_kid = student_name;
                                        top_grade = student_grade; 
                                }
                                else if(student_grade > second_top_grade && student_grade < top_grade){
                                        second_top_grade = student_grade; 
                                        second_top_kid = student_name; 
                                }
                                else{
                                        student_grade = 0.00; 
                                        student_name =""; 
                                }      
                        }
                        System.out.println("Top two students: ");
                        System.out.println( top_kid + "'s score is " + top_grade);
                        System.out.println(second_top_kid + "'s score is " + second_top_grade);
                }while(number_of_students >= 2);              
        }
    
        public static void main(String[] args){
                Find2Max test = new Find2Max(); 
                test.run(); 
        }        
    }
    

    我努力解决问题而且我非常接近但很困惑为什么我的程序似乎跳过而没有阅读我输入的学生姓名部分。当学生人数为1时,它似乎也有这个问题。任何帮助将不胜感激! :]

2 个答案:

答案 0 :(得分:1)

您需要使用Scanner.next();代替Scanner.nextLine();,因为nextLine方法skips the current line

答案 1 :(得分:0)

原始代码有很多问题:

  • 它不处理井线过渡
  • 它不处理第二个学生成绩高于第一个
  • 的情况
  • 它不处理等级平等的角落案例

以下是一个稍微改进的版本,带有一些额外的新行,使输出更具可读性。允许程序正常退出的修改是,如果提供的学生数量少于2,则退出,而不是人为地将数字设置为2。第3 else个语句被注释掉,不需要。

public class Find2Max {

  public void run() {
    Scanner sc = new Scanner(System.in);
    System.out.print("\nEnter the number of students (at least 2 required for comparison): ");
    int number_of_students = sc.nextInt();

    if(number_of_students < 2){
      System.out.println("\nThis program compares scores for 2 or more students.");
      // number_of_students = 2;
      return;
    }

    do{

      double top_grade = 0;
      double second_top_grade = 0;
      String top_kid = "";
      String second_top_kid= "";

      for(int ii = 0; ii < number_of_students; ii++){
        System.out.print("\nEnter a student name: ");
        String student_name = sc.next();

        System.out.print("Enter a student score: ");
        Double student_grade = sc.nextDouble();

        if(student_grade >= top_grade){
          if(top_grade >= second_top_grade) { // Save this grade as the second top
            second_top_kid = top_kid;
            second_top_grade = top_grade;
          }
          top_kid = student_name;
          top_grade = student_grade;
        }
        else if(student_grade > second_top_grade && student_grade < top_grade){
          second_top_grade = student_grade;
          second_top_kid = student_name;
        }
//        else{
//          student_grade = 0.00;
//          student_name ="";
//        }
      }
      System.out.println("\nTop two students: ");
      System.out.println( top_kid + "'s score is " + top_grade);
      System.out.println(second_top_kid + "'s score is " + second_top_grade);
    }while(number_of_students >= 2);
  }

  public static void main(String[] args){
    Find2Max test = new Find2Max();
    test.run();
  }
}