简体   繁体   中英

how to read the inputs in the scanner in java

I want to display the name of the student that has a higher gpa. I want to know how to limit the input in the scanner like I want to have just 2 students, so I'm gonna calculate their gap and return the one with the highest score, please help.

I used the method scanner as showed below

public double computerGPA() {

    double score;

    Scanner sc = new Scanner(System.in);
    System.out.println("enter the sutdent quiz score:");
    double score1 = sc.nextDouble();
    System.out.println("enter the student test score:");
    double score2 = sc.nextDouble();
    System.out.println("enter the student assigments score");
    double score3 = sc.nextDouble();
    sc.close();
    score = 0.2 * score1 + 0.5 * score2 + 0.3 * score3;

    return score;
   }
}
if((student1.computerGPA())<(student2.computerGPA())) {
    System.out.println("the student "+student2.name+"has higher GPA"); 
} 
if((student2.computerGPA())<(student1.computerGPA())) {
    System.out.println("the student "+student1.name+"has higher GPA"); 
} 
if((student1.computerGPA())==(student2.computerGPA())) { 
    System.out.println("the two students had the same GPA"); 
}

If i'm reading this right you're going to want a private double in your student class and instead of returning the score value to the program you can just have a void method which sets the students private double to that score

    private double studentScore;

        public static void computerGPA() {



    Scanner sc= new Scanner(System.in);
    System.out.println("enter the sutdent quiz score:");
    double score1=sc.nextDouble();
    System.out.println("enter the student test score:");
    System.out.println("enter the student assigments score");
    double score3=sc.nextDouble();
    sc.close();

    studentScore =0.2*score1+0.5*score2+0.3*score3;

then you can just have a get method for your two student objects and compare it that way

  if (student1.getGPAScore() > student2.getGPAScore()) {
  System.out.println("Student 1 has a higher score");
  } else {
   System.out.println("Student 2 has a higher score");
  }

Hope this makes sense

If you want comparison of only 2 student than you should use method computerGPA() only 2 times. You are limiting the input in the scanner by defining how menu times you use it. With your code:

if((student1.computerGPA())<(student2.computerGPA())) {
   System.out.println("the student "+student2.name+"has higher GPA"); 
} 
if((student2.computerGPA())<(student1.computerGPA())) {
   System.out.println("the student "+student1.name+"has higher GPA"); 
} 
if((student1.computerGPA())==(student2.computerGPA())) { 
   System.out.println("the two students had the same GPA"); 
}

because you call computerGPA() 6 times you are giving a user possibility to enter scores for 6 different students. You can solve this with:

public class Student {

    private String name;

    public Student(String name) {
        this.name = name;
    }

    public double computerGPA(Scanner sc) {
        System.out.println("Enter the QUIZ TEST ASSIGMENT scores for student " + name
            + " (for example: \"10 9 8\"):");

        /**A Scanner breaks its input into tokens using a delimiter pattern,
        which by default matches whitespace. 
        This way you can simplify input.
        But your code, where you ask specifically for every value, would also work.**/
        float score1 = sc.nextFloat();
        float score2 = sc.nextFloat();
        float score3 = sc.nextFloat();
        return 0.2 * score1 + 0.5 * score2 + 0.3 * score3;
    }

    public static void main(String[] args) {
        Student student1 = new Student("Boo");
        Student student2 = new Student("Foo");

        double student1ComputerGPA;
        double student2computerGPA;
        //Scanner must be outside computerGPA() method because of Aaron's comments.
        try (Scanner sc = new Scanner(System.in)) {
            //Call method computerGPA() only 2 times for 2 students.
            student1ComputerGPA = student1.computerGPA(sc);
            student2computerGPA = student2.computerGPA(sc);
        }

        //I use ternary operator instead of if() statements. Result is same.
        String result = student1ComputerGPA < student2computerGPA ?
            "The student \"" + student2.name + "\" has higher GPA."
            : (student2computerGPA < student1ComputerGPA ? 
                "The student \"" + student1.name + "\" has higher GPA." : 
                "The two students had the same GPA.");

        System.out.println(result);
    }
}
import java.util.Scanner;

public class Student{

    String name;
    boolean calculated;
    double score;

    Student(String name){
        this.name=name;
        this.calculated=false;
    }

    public double computerGPA() {

        if (!this.calculated) {

            Scanner sc = new Scanner(System.in);
            System.out.println("enter the sutdent quiz score:");
            double score1 = sc.nextDouble();
            System.out.println("enter the student test score:");
            double score2 = sc.nextDouble();
            System.out.println("enter the student assigments score");
            double score3 = sc.nextDouble();
            //sc.close();
            score = 0.2 * score1 + 0.5 * score2 + 0.3 * score3;
            this.calculated=true;

        }
        return score;

    }

    public static void main(String args[]) {

        Student student1= new Student("name1");
        Student student2=new Student("name2");


        if((student1.computerGPA())<(student2.computerGPA())) {
            System.out.println("the student "+student2.name+"has higher GPA"); 
        } 
        if((student2.computerGPA())<(student1.computerGPA())) {
            System.out.println("the student "+student1.name+"has higher GPA"); 
        } 
        if((student1.computerGPA())==(student2.computerGPA())) { 
            System.out.println("the two students had the same GPA"); 
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM