【问题标题】:how to read the inputs in the scanner in java如何在java中读取扫描仪中的输入
【发布时间】:2019-12-11 04:52:42
【问题描述】:

我想显示 gpa 较高的学生的姓名。 我想知道如何限制扫描仪中的输入,就像我只想有 2 个学生一样,所以我要计算他们的差距并返回得分最高的那个,请帮助。

我使用了如下所示的方法扫描器

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((student1.computerGPA())
  • 你能澄清一下“限制输入”是什么意思吗?当您致电computerGPA 时,您目前正在阅读 3 双打,这似乎已经仅限于我。如果我不得不猜测您的问题,我会说 1)一旦读取了 3 个双打,您应该添加一个 nextLine(),以便下一次调用 nextDouble() 从下一行读取和 2)您不应该关闭扫描仪,因为它关闭了底层输入流,这将使对该方法的第二次调用失败

标签: java java.util.scanner


【解决方案1】:

如果我没看错,你会在你的学生班级中想要一个私人双打,而不是将分数值返回给程序,你可以只使用一个 void 方法将学生私人双打设置为该分数

    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;

那么你可以为你的两个学生对象设置一个 get 方法并以这种方式进行比较

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

希望这是有道理的

【讨论】:

  • 当您第二次调用该方法时,System.in InputStream 已关闭并且调用sc.nextDouble() 将引发异常。此处示例:ideone.com/YXmjY7。一种解决方案是将扫描仪传递给 computerGPA/getGPAScore 方法,并让调用者管理扫描仪的创建和关闭
【解决方案2】:

如果您只想比较 2 个学生,那么您应该只使用方法 computerGPA() 2 次。您通过定义菜单使用时间的方式来限制扫描仪中的输入。使用您的代码:

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"); 
}

因为您拨打computerGPA() 6 次,您就可以让用户输入6 个不同学生的分数。您可以通过以下方式解决此问题:

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);
    }
}

【讨论】:

  • 问题是我希望代码在句子中提及学生姓名输入 Enter the student's QUIZ TEST ASSIGMENT score
  • @Kaoutar Bentalha 我编辑了我的答案。句子现在还包含特定学生的姓名。
【解决方案3】:
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"); 
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多