• 第二本书 二章(课后题)


    package com.opp.homework2.demo1;

    public class Demo1 {
    public void seasons(int num) {


    if (num == 2 || num == 3 || num == 4) {

    System.out.println("春季");
    }
    if (num == 5 || num == 6 || num == 7) {

    System.out.println("夏季");
    }
    if (num == 8 || num == 9 || num == 10) {

    System.out.println("秋季");
    }
    if (num == 12 || num == 11 || num == 1) {

    System.out.println("冬季");
    }
    }
    }
    
    
    package com.opp.homework2.demo1;

    import java.util.Scanner;

    public class MyMain {
    public static void main(String[] args) {
    Scanner scanner=new Scanner(System.in);
    System.out.println("请输入月份:");
    int number=scanner.nextInt();
    Demo1 demo1 =new Demo1();
    demo1.seasons(number);

    }
    }
    
    
    package com.opp.homework2.demo2;

    public class MyMain {
    public static void main(String[] args) {
    QuessMachine qm=new QuessMachine();
    qm.initial();
    qm.guess();

    }
    }
    package com.opp.homework2.demo2;

    import java.util.Scanner;

    public class QuessMachine {


    int num,guessNum;

    public void initial(){
    num=(int)(Math.random()*100);
    System.out.println(num);
    }
    public void guess(){
    Scanner scanner=new Scanner(System.in);
    boolean flag=false;
    do {
    System.out.println("请输入您的猜测价格:");
    guessNum = scanner.nextInt();
    if (guessNum > num) {
    System.out.println("再小些");
    } else if (guessNum < num) {
    System.out.println("再大些");
    } else {
    System.out.println("猜对了");
    flag=true;
    }
    }while (!flag);
    }
    }
    package com.opp.homework2.demo3;

    import java.util.Arrays;

    public class Demo3 {
    public void insertArray(int[] arr, int index, int value) {
    int[] newArr = Arrays.copyOf(arr, 8);
    for (int i = newArr.length - 1; i > index; i--) {
    newArr[i] = newArr[i - 1];
    }
    newArr[index] = value;
    for (int a : newArr) {
    System.out.println(a);
    }
    }
    }
    /*未想完
    package com.opp.homework2.demo3;

    import java.util.Scanner;

    public class MyMain {
    public static void main(String[] args) {
    Demo3 demo3 = new Demo3();
    int[] arr = {3, 64, 15, 36, 72, 38, 29};
    for (int b:arr) {
    System.out.println(b);
    }
    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入要插入的位置:");
    int index = scanner.nextInt();
    System.out.println("请输入要插入的数字:");
    int value = scanner.nextInt();
    demo3.insertArray(arr, index, value);
    }
    }
    */
    
    
    package com.opp.homework2;

    import java.util.Scanner;

    public class MyMain {

    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    while (in.hasNextInt()) {
    int n = in.nextInt();
    int m = in.nextInt();

    Student[] stu = new Student[n]; //创建n个学生对象的数组
    double[] avg = new double[m]; //各科的平均成绩
    int i, j;
    int sum, num = 0;

    for (i = 0; i < n; i++) {
    sum = 0;
    stu[i] = new Student(m);
    for (j = 0; j < m; j++) { //依次读入一个学生的各科成绩
    stu[i].grade[j] = in.nextInt();
    sum += stu[i].grade[j]; //一个学生实例的成绩总和
    avg[j] += stu[i].grade[j]; //各科的总成绩
    }
    stu[i].setAvg(sum * 1.0 / m); //设置该学生的平均成绩
    }
    for (j = 0; j < m; j++) { //各科的总成绩取平均
    avg[j] /= n;
    }

    boolean flag;
    //计算各科成绩均大于等于平均成绩的学生数量
    for (i = 0; i < n; i++) {
    flag = true;
    for (j = 0; j < m; j++) {
    if (stu[i].grade[j] < avg[j]) {
    flag = false;
    break;
    }
    }

    if (flag) {
    num++;
    }
    }


    for (i = 0; i < n - 1; i++) {
    System.out.printf("%.2f ", stu[i].avg);
    }
    System.out.printf("%.2f", stu[n - 1].avg);
    System.out.println();
    for (i = 0; i < m - 1; i++) {
    System.out.printf("%.2f ", avg[i]);
    }
    System.out.printf("%.2f", avg[m - 1]);
    System.out.println();
    System.out.println(num);
    System.out.println();
    }

    }

    public static class Student {
    double avg;
    double[] grade;

    //Student类的构造函数
    public Student(int m) {
    grade = new double[m]; //实例化每个Student的成绩数组
    }

    public void setAvg(double avg) {
    this.avg = avg;
    }
    }

    }
    package com.opp.unit2.homework2.demo4;

    import java.util.Scanner;

    public class MyMain {
    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    String chioce;
    StudentBiz studentBiz = new StudentBiz();

    do {
    /**
    * 写入数值,带入数组
    */
    Student student = new Student();
    System.out.println("请输入学号:");
    student.id=scanner.nextInt();
    System.out.println("请输入JAVA成绩:");
    student.javaScores = scanner.nextDouble();
    System.out.println("请输入C3成绩:");
    student.CScores = scanner.nextDouble();
    System.out.println("请输入DB成绩:");
    student.dbScores = scanner.nextDouble();
    studentBiz.addScores(student);
    System.out.println("是否继续?(y/n)");
    chioce = scanner.next();
    } while ("y".equals(chioce));
    studentBiz.showavgs();
    }
    }
    package com.opp.unit2.homework2.demo4;

    public class Student {
    /**
    * 定义学生属性
    */

    public double javaScores;
    public double CScores;
    public double dbScores;
    public double avg;
    public int id;

    /**
    * 定义显示学生方法
    */
    public void showInfo() {
    System.out.println("学生"+id+"平均成绩为:" + (avg = (javaScores + CScores + dbScores) / 3));
    }
    }
    package com.opp.unit2.homework2.demo4;


    public class StudentBiz {
    /**
    * 学生管理类
    */
    Student[] avgs = new Student[10];

    /**
    * 添加学生信息(成绩)
    *
    * @param avg
    * @date 将学生数据作为元素存入数组(变成参数插入)
    */
    public void addScores(Student avg) {
    for (int i = 0; i < avgs.length; i++) {
    if (avgs[i] == null) {
    avgs[i] = avg;
    break;
    }
    }
    }

    /**
    * 展示每个学生的3科平均分
    */
    public void showavgs() {
    System.out.println("学生平均分为:");
    for (int i = 0; i < avgs.length; i++) {
    if (avgs[i] != null) {
    avgs[i].showInfo();
    }
    }
    }


    }



    
    
  • 相关阅读:
    Unity3d与Android交互
    A star 寻路
    网络协议
    数据驱动
    有限状态机(FSM)
    自己封装一个Log模块
    Unity5.x版本AssetBundle加载研究
    Unity5.x版本AssetBundle打包研究
    alidoing --使用JS实现多语言框架、喜欢的请进、、瓦特平台!
    使用代码生成器“代码工厂”快速生成B/S程序代码
  • 原文地址:https://www.cnblogs.com/yunfeioliver/p/9130063.html
Copyright © 2020-2023  润新知