远程点亮一个LED 灯是物联网最基本的实验,假如灯的位置用一个点的从标来表示,请设计一个点类 ,并针对这个类进行编程实验,当输入位置坐标和开关状态时输出Led灯的 on或off状态。
输入格式:
坐标和开关状态,如x y true
输出格式:
灯的开关状态 on或off
输入样例:
在这里给出一组输入。例如:
1 1 false
输出样例:
在这里给出相应的输出。例如:
off
import java.util.*;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
int la=s.nextInt(),lo=s.nextInt();
boolean bo=s.nextBoolean();
GeographicInfo G;
G = new GeographicInfo(la,lo,bo);
// G.Led();
}
}
class GeographicInfo {
int latitude;
int longitude;
boolean bo;
public GeographicInfo(int latitude,int longitude,boolean bo){
this.latitude=latitude;
this.longitude=longitude;
this.bo=bo;
Led();
}
public void Led(){
if(bo==false)
System.out.println("off");
else System.out.println("on");
}
}
思路:
设计一个点类GeographicInfo,类中含有三个成员latitude、longitude和bo分别存储灯的位置以及灯的状态。通过Led()方法对自身的bo成员进行检测,如果为true时输出状态on,否则输出off