多语言展示
当前在线:1414今日阅读:103今日分享:49

java中怎么实现键盘录入的功能?

提出问题:java开发中,常常需要从键盘录入一些字符和数字。怎么实现呢?解决问题:实例说明,从键盘上录入学生姓名,科目,成绩,系统自动评定等级。
工具/原料
1

1.开发平台:Intellij IDEA 2016.2

2

2.实现关键代码: Scanner input =new Scanner(System.in);

方法/步骤
1

step1:新建一个演示类demo

2

step2:导入 包文件,在包名下,类名之上输入如下代码。import  java.util.Scanner;

3

step3:在类中的代码如下:public static void main(String[] args) {    //创建一个键盘录入对象input    Scanner input = new Scanner(System.in);    System.out.println('please input “学生姓名”');    String studentName = input.next().intern();    System.out.println('please input “科目名称”');    String subject = input.next().intern();    System.out.println('please input“科目成绩”');    double result = input.nextDouble();    //调用Student类的方法    Student stu = new Student();    stu.setStudentName(studentName);    stu.setSubject(subject);    stu.setResult(result);    Student.getInformation(stu);}

4

step4:新建一个Student类,设置类的各个成员变量,创建一个学生个人信息的方法。如下:public class Student {    private String studentName;    private String subject;    private double result;    private String eveluate;    //创建一个信息输出方法    public static void getInformation(Student studentInformation) {        System.out.println('学生个人信息');        //获取学生姓名返回的成员变量值        System.out.println('姓名:' + studentInformation.getStudentName());        //获取科目成员变量的返回值        System.out.println('科目:' + studentInformation.getSubject());        //获取成绩成员变量的返回值        System.out.println('成绩:' + studentInformation.getResult());        //获取等级成员变量的返回值        System.out.println('等级:' + studentInformation.getEveluate());    }    //使用getXxx()和setXxx()对各个私有成员变量进行限定    //对学生姓名进行输入和输出的设置    public String getStudentName() {        return this.studentName;    }    public void setStudentName(String studentName) {        this.studentName = studentName;    }    //对成绩等级变量设置    public String getEveluate() {        return this.eveluate;    }    public void setEveluate(String eveluate) {        this.eveluate = eveluate;    }     //对科目成员变量进行设置    public String getSubject() {        return this.subject;    }    public void setSubject(String subject) {        this.subject = subject;    }    public double getResult() {        return this.result;    }    //对成绩进行等级划分    public void setResult(double result) {        if (result >= 95) {            this.result = result;            this.eveluate = 'A+';        } else if (result < 90 && result >= 85) {            this.result = result;            this.eveluate = 'A';        } else if (result >= 80 && result < 85) {            this.result = result;            this.eveluate = 'B+';        } else if (result >= 75 && result < 80) {            this.result = result;            this.eveluate = 'B';        } else if (result >= 70 && result < 75) {            this.result = result;            this.eveluate = 'C+';        } else if (result >= 60 && result < 70) {            this.result = result;            this.eveluate = 'C';        } else if (result >= 50 && result < 60) {            this.result = result;            this.eveluate = 'D';        } else {            this.result = result;            this.eveluate = 'E';        }    }}

5

运行结果1:please input “学生姓名”李小明please input “科目名称”数学please input“科目成绩”98学生个人信息姓名:李小明科目:数学成绩:98.0等级:A+运行结果2:please input “学生姓名”王强please input “科目名称”英语please input“科目成绩”52学生个人信息姓名:王强科目:英语成绩:52.0等级:D。。。。。。

推荐信息