用Java 程序写,使用 Scanner 类实现从键盘输入学生的姓名、数学成绩和英语成绩,然后计算并输出该学生的平均分
时间: 2023-05-19 17:01:22 浏览: 144
好的,以下是用 Java 程序实现从键盘输入学生的姓名、数学成绩和英语成绩,然后计算并输出该学生的平均分的代码:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入学生的姓名:");
String name = scanner.nextLine();
System.out.print("请输入学生的数学成绩:");
double mathScore = scanner.nextDouble();
System.out.print("请输入学生的英语成绩:");
double englishScore = scanner.nextDouble();
double averageScore = (mathScore + englishScore) / 2;
System.out.println(name + "的平均分是:" + averageScore);
}
}
```
希望这个代码能够帮到你!
相关问题
用java实现以下1、使用Scanner类解析出“数学87分,物理76分,英语96分”的成绩,计算总成绩和平均成绩。 2、输出某年某月的日历页,通过键盘输入年份和月份。 3、计算某年某月某日和某年某月某日之间的天数间隔,要求年月日通过键盘输入到程序中。(参考课本P249 Example9.18) (通过键盘输入数据参考课本P25 2.3从命令行输入与输出数据)
1. 使用Scanner类解析出“数学87分,物理76分,英语96分”的成绩,计算总成绩和平均成绩。
```
import java.util.Scanner;
public class ScoreCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩(格式为:数学xx分,物理xx分,英语xx分):");
String input = scanner.nextLine();
// 解析字符串,获取数学、物理、英语成绩
String[] scores = input.split(","); // 注意这里用的是中文逗号
int mathScore = Integer.parseInt(scores[0].substring(2, scores[0].length() - 1));
int physicsScore = Integer.parseInt(scores[1].substring(2, scores[1].length() - 1));
int englishScore = Integer.parseInt(scores[2].substring(2, scores[2].length() - 1));
// 计算总成绩和平均成绩
int totalScore = mathScore + physicsScore + englishScore;
double avgScore = ((double) totalScore) / 3;
System.out.println("总成绩为:" + totalScore + "分");
System.out.println("平均成绩为:" + avgScore + "分");
}
}
```
2. 输出某年某月的日历页,通过键盘输入年份和月份。
```
import java.util.Calendar;
import java.util.Scanner;
public class CalendarPrinter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入年份:");
int year = scanner.nextInt();
System.out.println("请输入月份:");
int month = scanner.nextInt();
// 获取该年该月的第一天
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, 1);
// 打印日历页
System.out.println("-----------------------------");
System.out.println(" " + year + "年" + month + "月");
System.out.println("-----------------------------");
System.out.println("日 一 二 三 四 五 六");
// 打印该月的日历
int weekday = calendar.get(Calendar.DAY_OF_WEEK) - 1;
int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
for (int i = 0; i < weekday; i++) {
System.out.print(" ");
}
for (int i = 1; i <= maxDay; i++) {
System.out.printf("%2d ", i);
if ((weekday + i) % 7 == 0) {
System.out.println();
}
}
if ((weekday + maxDay) % 7 != 0) {
System.out.println();
}
System.out.println("-----------------------------");
}
}
```
3. 计算某年某月某日和某年某月某日之间的天数间隔,要求年月日通过键盘输入到程序中。
```
import java.util.Calendar;
import java.util.Scanner;
public class DayIntervalCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入第一个日期(格式为:年 月 日):");
int year1 = scanner.nextInt();
int month1 = scanner.nextInt();
int day1 = scanner.nextInt();
System.out.println("请输入第二个日期(格式为:年 月 日):");
int year2 = scanner.nextInt();
int month2 = scanner.nextInt();
int day2 = scanner.nextInt();
// 计算两个日期的天数间隔
Calendar calendar1 = Calendar.getInstance();
calendar1.set(year1, month1 - 1, day1);
long time1 = calendar1.getTimeInMillis();
Calendar calendar2 = Calendar.getInstance();
calendar2.set(year2, month2 - 1, day2);
long time2 = calendar2.getTimeInMillis();
long interval = Math.abs((time2 - time1) / (1000 * 60 * 60 * 24));
System.out.println("两个日期的天数间隔为:" + interval + "天");
}
}
```
1. 设计Person类, (1)包含姓名(String)、性别(String)和年龄(int)成员变量,以及相应的getter和setter方法。 (2) 至少实现一个构造方法 (3)重写toString方法,输出Person对象信息; 2. 以Person为父类,设计一个学生类Student, (1)增加学号(int)、班级(String)、英语成绩、数学成绩和java成绩 这几个成员变量,以相应的getter和setter方法。 (2) 至少实现一个构造方法 (3) 重写toString()方法,输出Student对象信息。 3. 设计一个测试类StudentDemo,包含成员变量List<Student> students; 在main()方法中,调用并实现: (1)添加学生方法addStudents,实现从键盘输入学生信息,创建学生对象,添加到students 。 (2)删除学生方法delStudents,根据从键盘输入的学号信息,从students 删除该学生对象。 (3)输入成绩方法inputScore,从键盘为不同学生输入英语、数学和java成绩。 (4)输出成绩方法outScore, 输出students中所有学生的基本信息、3门课程成绩和平均成绩。 运行StudentDemo程序,实现添加同学、输入成绩、输出成绩、删除同学的功能测试。
1. Person类的代码:
public class Person {
private String name;
private String gender;
private int age;
public Person() {}
public Person(String name, String gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", gender=" + gender + ", age=" + age + "]";
}
}
2. 学生类Student的代码:
public class Student extends Person {
private int stuNum;
private String className;
private double engScore;
private double mathScore;
private double javaScore;
public Student() {}
public Student(String name, String gender, int age, int stuNum, String className, double engScore, double mathScore, double javaScore) {
super(name, gender, age);
this.stuNum = stuNum;
this.className = className;
this.engScore = engScore;
this.mathScore = mathScore;
this.javaScore = javaScore;
}
public int getStuNum() {
return stuNum;
}
public void setStuNum(int stuNum) {
this.stuNum = stuNum;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public double getEngScore() {
return engScore;
}
public void setEngScore(double engScore) {
this.engScore = engScore;
}
public double getMathScore() {
return mathScore;
}
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
}
public double getJavaScore() {
return javaScore;
}
public void setJavaScore(double javaScore) {
this.javaScore = javaScore;
}
public double getAverageScore() {
return (engScore + mathScore + javaScore) / 3.0;
}
@Override
public String toString() {
return "Student [name=" + getName() + ", gender=" + getGender() + ", age=" + getAge() + ", stuNum=" + stuNum + ", className="
+ className + ", engScore=" + engScore + ", mathScore=" + mathScore + ", javaScore=" + javaScore + ", averageScore="
+ getAverageScore() + "]";
}
}
3. StudentDemo测试类的代码:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class StudentDemo {
private List<Student> students = new ArrayList<Student>();
// 添加学生方法
public void addStudents() {
Scanner sc = new Scanner(System.in);
System.out.print("请输入姓名:");
String name = sc.next();
System.out.print("请输入性别:");
String gender = sc.next();
System.out.print("请输入年龄:");
int age = sc.nextInt();
System.out.print("请输入学号:");
int stuNum = sc.nextInt();
System.out.print("请输入班级:");
String className = sc.next();
Student student = new Student(name, gender, age, stuNum, className, 0, 0, 0); // 初始成绩为0
students.add(student);
System.out.println("添加成功!");
}
// 删除学生方法
public void delStudents() {
Scanner sc = new Scanner(System.in);
System.out.print("请输入要删除的学生的学号:");
int stuNum = sc.nextInt();
Iterator<Student> it = students.iterator();
while (it.hasNext()) {
Student s = it.next();
if (s.getStuNum() == stuNum) {
it.remove();
System.out.println("删除成功!");
return;
}
}
System.out.println("该学号不存在!");
}
// 输入成绩方法
public void inputScore() {
Scanner sc = new Scanner(System.in);
System.out.print("请输入要输入成绩的学生的学号:");
int stuNum = sc.nextInt();
for (Student s : students) {
if (s.getStuNum() == stuNum) {
System.out.print("请输入英语成绩:");
double engScore = sc.nextDouble();
System.out.print("请输入数学成绩:");
double mathScore = sc.nextDouble();
System.out.print("请输入Java成绩:");
double javaScore = sc.nextDouble();
s.setEngScore(engScore);
s.setMathScore(mathScore);
s.setJavaScore(javaScore);
System.out.println("成绩输入成功!");
return;
}
}
System.out.println("该学号不存在!");
}
// 输出成绩方法
public void outScore() {
System.out.println("所有学生的信息如下:");
for (Student s : students) {
System.out.println(s);
}
}
public static void main(String[] args) {
StudentDemo demo = new StudentDemo();
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入选择操作的序号:1、添加学生 2、删除学生 3、输入成绩 4、输出成绩 5、退出");
int operateNum = sc.nextInt();
switch (operateNum) {
case 1:
demo.addStudents();
break;
case 2:
demo.delStudents();
break;
case 3:
demo.inputScore();
break;
case 4:
demo.outScore();
break;
case 5:
System.out.println("程序已退出!");
return;
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
}
阅读全文