第三章类与对象基础 ② 代码

1.课前测试

使用java循环输入5个人的分数,求总分,平均分,要求:
1.所有分数都输入正确,才输出总分,平均分
2.有任意分数错误,则终止循环,输出错误提示

代码如下:

package com.yzh70627;

/**
 * @author: XYT
 * @create-date: 2022/6/27 8:54
 */
import java.lang.reflect.Array;
import java.util.Scanner;
public class ceshi {
    public static void main(String[] args) {
        //使用java循环输入5个人的分数,求总分,平均分,要求:
        //1.所有分数都输入正确,才输出总分,平均分
        //2.有任意分数错误,则终止循环,输出错误提示
        double sum = 0;
        double max=0;
        double min=0;
        boolean flag=true;  //设定终止判断条件标志!!!! 一慌就想不起来
        for (int i = 1; i <= 5; i++) {
            System.out.println("请输入第"+i+"次成绩");
            Scanner sc = new Scanner(System.in);
            double score = sc.nextDouble();

            if(score>100 || score<0){
                System.out.println("输出错误!");
                flag=false;
                break ;
            }
            sum+=score;

            if(i==1){
                max=score;
                min=score;
            }

            if (max<score){
                max=score;
            }

            if (min>score){
                min=score;
            }

        }
        if(flag==true){
            System.out.println("和是"+sum+",平均值是"+sum/5+"最大值"+max+"最小值"+min);
        }

//                int i;
//                int sum=0;
//                for(i=1;i<=5;i++){
//                    Scanner sc = new Scanner(System.in);
//                    System.out.println("请输入第"+i+"个学生分数");
//                    int price = sc.nextInt();
//                    sum += price;
//                    if(price>100||price<0){
//                        System.out.println("输入成绩有误");
//                        return;
//                    }
//
//                }
//                System.out.println("总分为"+sum);
//                System.out.println("平均分为"+sum/5);
            }
            
}

运行结果如下:

在这里插入图片描述

package com.yzh70627;
import java.util.Scanner;
/**
 * @author: XYT
 * @create-date: 2022/6/27 20:06
 */
public class boke {

        public static void main(String[] args) {
            //创建输入工具类对象
            Scanner scanner = new Scanner(System.in);
            int sum = 0;
            //定义标记变量,假设输入都是对的
            boolean flag = true;
            for (int i=1;i<=5;i++){
                System.out.println("请输入第"+i+"个学生的成绩:");
                try {
                    int score = scanner.nextInt();
                    if(score<0 || score>100){
                        System.out.println("输入分数有误");
                        flag=false;
                        break;
                    }
                    sum+=score;
                }catch (Exception ex){
                    System.out.println("输入不合法");
                    flag=false;
                    break;
                }
            }

            if(flag==true) {
                System.out.println("分数和:" + sum + " 平均分:" + (sum / 5.0));
            }
        }

}

运行结果如下:
在这里插入图片描述

package com.yzh70627;
import java.util.Scanner;
/**
 * @author: XYT
 * @create-date: 2022/6/27 20:06
 */
public class boke {
    
        public static void main(String[] args) {
            //输入分数,求最大值,最小值,统计80分以上的学生比例

            //创建输入工具类对象
            Scanner scanner = new Scanner(System.in);
            //求和
            int sum = 0;
            //最大值
            int max = 0;
            //最小值
            int min = 1000;
            //记录80以上的人数
            int count = 0;
            //定义标记变量,假设输入都是对的
            boolean flag = true;
            for (int i=1;i<=5;i++){
                System.out.println("请输入第"+i+"个学生的成绩:");
                int score = scanner.nextInt();
                if(score<0 || score>100){
                    System.out.println("输入分数有误");
                    flag=false;
                    break;
                }
                sum+=score;

                //比较最大值
                if (score>max){
                    max=score;
                }
                //比较最小值
                if(score<min){
                    min=score;
                }

                //统计80分以上的比例
                if (score<80){
                    continue;
                }
                count++;
            }

            if(flag==true) {
                System.out.println("分数和:" + sum + " 平均分:" + (sum / 5.0));
                System.out.println("最大值:"+max+" 最小值:"+min+" 80分以上比例:"+((count/5.0)*100)+"%");
            }
        }
    
}

运行结果如下:
在这里插入图片描述

2.定义学生 成员方法

代码如下:

package com.yzh70627;

/**
 * @author: XYT
 * @create-date: 2022/6/27 20:06
 */
public class Student {
    //属性(定义变量):
    //姓名
    String name;
    //性别
    String sex;
    //年龄
    int age;
    //身高
    double tall;
    //学校
    String school;

    //方法/行为(定义方法):
    //吃饭
    public void eat(){
        //this:表示调用方法的当前对象
        System.out.println(this.name+"在吃早饭");
    }

    //学习
    public void study(String course){
        System.out.println(this.school+"的学生"+this.name+"学习"+course);
    }
}

package com.yzh70627;

/**
 * @author: XYT
 * @create-date: 2022/6/27 20:06
 */
public class Test {
    public static void main(String[] args) {
        //普通变量的使用
        //定义类型变量,赋值
        int age = 20;

        //2.创建学生类的对象
        //定义类型变量,赋值一个类型的对象
        Student stu = new Student();
        //为对象属性赋值
        stu.name="张三";
        stu.sex="男";
        stu.age=20;
        stu.tall=1.7;
        stu.school="清华";
        //调用对象的方法
        stu.eat();
        stu.study("java");

        //创建第二个学生
        Student stu2 = new Student();
        stu2.name="李四";
        stu2.sex="女";
        stu2.age=18;
        stu2.tall=1.6;
        stu2.school="北大";
        stu2.eat();
        stu2.study("html");


    }
}

运行结果如下:
#pic_center

2.定义人 构造方法(类名和方法名一致)

代码如下:

package com.yzh70627;

/**
 * @author: XYT
 * @create-date: 2022/6/27 20:06
 */
public class People {
    //对象属性其实就是成员变量/全局变量,不赋值的时候有默认值
    // String : null 整数 :0 小数:0.0 布尔:false

    String name;
    String sex;
    int age;
    String address;

    //无参构造:1.方法名与类名完全一样 2.没有返回值,不能写void
    public  People(){
        System.out.println("调用了People的无参构造");
    }
    //有参构造:1.在构造方法中定义参数 2.用于为对象的属性赋值
    public People(String name,String sex,int age,String address){
        this.name=name;
        this.sex=sex;
        this.age=age;
        this.address=address;
    }

    //有参构造
    public People(String xm,String xb){
        name=xm;
        sex=xb;
    }
}
package com.yzh70627;

/**
 * @author: XYT
 * @create-date: 2022/6/27 20:06
 */
public class Test {
    public static void main(String[] args) {
        //new People():当类中没有定义构造时,系统会提供默认的无参构造
        //             一旦在类中定义构造,则必须调用自己的构造
//        new People();

//        People p = new People();
//        System.out.println(p.name);
//        System.out.println(p.sex);
//        System.out.println(p.age);
//        System.out.println(p.address);

        //调用无参构造创建对象,一个一个赋值属性时,容易漏掉某个属性
        /*People p = new People();
        p.name="张三";
        p.sex="男";
        System.out.println(p.name+" "+p.sex+" "+p.age+" "+p.address);
         */


        //调用有参构造:必须按参数个数传值,避免漏掉某些属性
        People p = new People("张三","男",18,"河南");
        System.out.println(p.name+" "+p.sex+" "+p.age+" "+p.address);

        //new People();
    }
}

运行结果如下:
#pic_center

4.定义手机 成员方法

题目:
请定义一个手机类
1.定义属性: 品牌,价格,颜色
2.定义方法:充电,打电话
3.定义构造
4.创建测试类。在测试类main方法中,创建手机类的对象,并设置属性
5.调用对象的方法完成测试

代码如下:

package com.yzh70627;

/**
 * @author: XYT
 * @create-date: 2022/6/27 20:06
 */
public class Phone {
    //定义属性
    //品牌
    String brand;
    //价格
    int price;
    //颜色
    String color;

    public Phone(){

    }

    public Phone(String brand,int price,String color){
        this.brand=brand;
        this.price=price;
        this.color=color;
    }

    public void chongdian(){
        System.out.println("给"+this.brand+"的手机充电");
    }

    public void call(){
        System.out.println("使用价值"+this.price+"的"+this.color+"的"+this.brand+"手机给别人打电话");
    }
}
package com.yzh70627;

/**
 * @author: XYT
 * @create-date: 2022/6/27 20:06
 */
public class Test {
    public static void main(String[] args) {
        Phone p1 = new Phone();
        p1.brand="小米";
        p1.color="红色";
        p1.price=2000;

        p1.chongdian();
        p1.call();

        Phone p2 = new Phone("华为",3000,"黑色");
        p2.chongdian();
        p2.call();
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值