首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用toString()方法的Fan探测--简单

使用toString()方法的Fan探测--简单
EN

Stack Overflow用户
提问于 2014-11-19 07:43:08
回答 1查看 1.5K关注 0票数 0

我正在尝试创建一个程序,您可以在其中更改风扇的速度,颜色,半径,无论您是否可以打开或关闭风扇,我让风扇类工作正常,除了toString()方法,由于某些原因,当我在测试程序中设置一些值时,它只是默认为常规值,接受任何帮助。

谢谢。

代码语言:javascript
复制
public class Fan {
final int SLOW = 1;
final int MEDIUM = 2;
final int FAST = 3;

public int speed = SLOW;
public boolean on = false;
public double radius = 5;
public String color = new String("blue");
//fan on
boolean fanOn() {
   on = true;
   return on;
}
//fan off
boolean fanOff() {
   on = false;
   return on;
}
//sets fan speed
String setSpeed(String speed) {
if (speed == "SLOW"){
   speed = String.valueOf(SLOW);
} else if (speed == "MEDIUM") {
   speed = String.valueOf(MEDIUM);
} else if (speed == "FAST") {
   speed = String.valueOf(FAST);
} else {
   speed = "Please enter 'SLOW', 'MEDIUM' or 'FAST' to change the speed of the fan.";
}
   return speed;
}
//sets custom radius
double setRadius(double rad) {
   rad = radius;
   return rad;
}
//sets custom color
String setColor(String colorType) {
   return colorType;
}
//toString() method
public String toString() {
   return ("Speed: " + speed + "\nRadius: " + radius + "\nColor: " + "\nOn: " + on);
 }
}

//test program

 public class TestFan {
 public static void main(String[] args) {
   Fan fan1 = new Fan();
   fan1.setColor("green");
   fan1.setSpeed("FAST");
   fan1.setRadius(3.5);
   fan1.fanOff();
   System.out.println(fan1.toString());

 }
}

这只是输出:

代码语言:javascript
复制
Speed: 1
Radius: 5.0
Color: 
On: false
EN

回答 1

Stack Overflow用户

发布于 2014-11-19 07:46:14

代码语言:javascript
复制
public class Fan {
final int SLOW = 1;
final int MEDIUM = 2;
final int FAST = 3;

public int speed = SLOW;
public boolean on = false;
public double radius = 5;
public String color = new String("blue");
//fan on
void fanOn() {
   on = true;
   return on;
}
//fan off
void fanOff() {
   on = false;
   return on;
}
//sets fan speed
void setSpeed(String speed) {
   this.speed=speed;
}
//sets custom radius
double setRadius(double rad) {
   rad = radius;
   return rad;
}
//sets custom color
void setColor(String colorType) {
   color = colorType;
}
//toString() method
public String toString() {
   return ("Speed: " + speed + "\nRadius: " + radius + "\nColor: " + "\nOn: " + on);
 }
}

//test program

 public class TestFan {
 public static void main(String[] args) {
   Fan fan1 = new Fan();
   fan1.setColor("green");
   fan1.setSpeed("FAST");
   fan1.setRadius(3.5);
   fan1.fanOff();
   System.out.println(fan1.toString());

 }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27006339

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档