Java-PTA 物联网远程点灯实验
PTA 物联网远程点灯实验
·
一、题目
二、算法思想
设计一个点类
Point
,类中含有三个成员x
、y
和state
分别存储灯的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");
}
}
}
更多推荐
所有评论(0)