一、题目

在这里插入图片描述

二、算法思想

设计一个点类Point,类中含有三个成员xystate分别存储灯的x、y位置以及灯的状态。
通过check()方法对自身的state成员进行检测,如果为true时输出状态on,否则输出off
在构造方法Point(int x,int y,boolean state)中先将传入的三个形参赋值到对应的三个成员中
然后调用自身类的check()方法对state进行检测。

程序执行的过程如下:

  • 输入变量x、y和state
  • 实例化Point类的对象point,触发构造函数Point(int x,int y,boolean state)
  • 构造函数又触发check()方法输出on或off

三、代码

在这里插入图片描述

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int x = scanner.nextInt();
        int y = scanner.nextInt();
        boolean state = scanner.nextBoolean();
        Point point = new Point(x,y,state);
    }
}
class Point{//点类
    int x,y;
    boolean state;
    public Point(){//构造方法
    }
    public Point(int x,int y,boolean state){//构造方法
        this.x = x;
        this.y = y;
        this.state = state;
        check();
    }
    public void check(){//检测开关情况,并输出状态
        if(this.state==true){
            System.out.println("on");
        }
        else{
            System.out.println("off");
        }
    }
}
Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐