Java 接受参数并返回布尔值的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18538466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Methods to accept parameters and return a boolean
提问by Gibbo
A bit lost here, nothing appears to be working. I have read back on the chapter about return methods and I just can't get it to work.
这里有点迷失,似乎没有任何工作。我已经阅读了关于返回方法的章节,但我无法让它工作。
Basically I have to create a method that accepts a Park paremeter, I have a class called park that has setters and getters for the booleans, picnic, playground, tennis and pool, which basically get declared as true or false if the Park has them. It also needs return a boolean value if the park has picnic and playground facilities.
基本上,我必须创建一个接受 Park 参数的方法,我有一个名为 park 的类,它具有布尔值、野餐、游乐场、网球和游泳池的 setter 和 getter,如果 Park 有它们,它们基本上会被声明为 true 或 false。如果公园有野餐和游乐场设施,它还需要返回一个布尔值。
Here is what I have so far:
这是我到目前为止所拥有的:
The Park class:
公园类:
public class Park {
private String parkName;
private int parkAcres;
private boolean picnicFacilities;
private boolean tennisCourt;
private boolean playground;
private boolean swimmingPool;
public String getParkName() {
return parkName;
}
public void setParkName(String parkName) {
this.parkName = parkName;
}
public int getParkAcres() {
return parkAcres;
}
public void setParkAcres(int parkAcres) {
if (parkAcres < 0 || parkAcres > 400) {
System.out
.println("You have entered an invalid acre amount, it has been defaulted to 0");
parkAcres = 0;
}
this.parkAcres = parkAcres;
}
public boolean isPicnicFacilities() {
return picnicFacilities;
}
public void setPicnicFacilities(boolean picnicFacilities) {
this.picnicFacilities = picnicFacilities;
}
public boolean isTennisCourt() {
return tennisCourt;
}
public void setTennisCourt(boolean tennisCourt) {
this.tennisCourt = tennisCourt;
}
public boolean isPlayground() {
return playground;
}
public void setPlayground(boolean playground) {
this.playground = playground;
}
public boolean isSwimmingPool() {
return swimmingPool;
}
public void setSwimmingPool(boolean swimmingPool) {
this.swimmingPool = swimmingPool;
}
}
The exercise told me to create getters and setters for all of them, I haven't even got passed using one of them yet.
练习告诉我为所有这些创建 getter 和 setter,我什至还没有通过使用它们中的一个。
Here is the ParkTest class which has the methods to use these getters and setters:
这是 ParkTest 类,它具有使用这些 getter 和 setter 的方法:
import java.util.Scanner;
public class TestParks {
static Park park1 = new Park();
static String parkName;
static int parkAcres;
static boolean facilityCheck;
public static void main(String[] args) {
parkInfo(park1);
request();
System.out.println("Test: " + facilityCheck);
}
public static void parkInfo(Park park) {
Scanner userInput = new Scanner(System.in);
System.out.println("Enter the park name");
parkName = userInput.next();
System.out.println("Enter the size in acres");
parkAcres = userInput.nextInt();
park.setParkName(parkName);
park.setParkAcres(parkAcres);
}
public static boolean request() {
Scanner userInput = new Scanner(System.in);
System.out.println("Do you want picnic facilities?");
if(userInput.nextInt() == 1){
park1.setPicnicFacilities(true);
return true;
}else{
return false;
}
}
}
I don't quite get what the exercise wants me to do, it is asking me to check if both facilities are available and return the value. Everything I have tried, the value just ends up false every time. The book taught me this back in a chapter but really, it only shows the use with int/double variable and not boolean, no idea if this makes a difference, either way I am completely lost on how to do return methods...
我不太明白练习要我做什么,它要求我检查两个设施是否可用并返回值。我尝试过的一切,每次都以错误结束。这本书在一章中教了我这个,但实际上,它只显示了使用 int/double 变量而不是布尔值,不知道这是否有区别,无论哪种方式,我都完全不知道如何执行返回方法......
Normally I would just prompt the user for what they want in the park, then use the setters to declare the booleans true, then use a simple if statement in a displayParkInfo() method using the getters to output the results to the console, it seems the task does not want me to do this though.
通常我只会提示用户在公园里他们想要什么,然后使用 setter 声明布尔值 true,然后在 displayParkInfo() 方法中使用一个简单的 if 语句使用 getter 将结果输出到控制台,看起来任务不希望我这样做。
采纳答案by Sotirios Delimanolis
A class field is by default initialized to null
for reference types, 0
for numeric types, and false
for boolean types. Your field
类字段默认初始化null
为引用类型、0
数字类型和false
布尔类型。你的领域
static boolean facilityCheck; // = false
will be initialized to false
. And since you aren't re-initializing it anywhere so it stays that way. You need to either set it to the return value of request()
将被初始化为false
. 而且由于您没有在任何地方重新初始化它,所以它保持这种状态。您需要将其设置为的返回值request()
facilityCheck = request();
or have that method itself set its value.
或者让该方法本身设置其值。
if(userInput.nextInt() == 1){
ark1.setPicnicFacilities(true);
facilityCheck = true;
} else{
facilityCheck = false;
}
// return or not (change your method definition if not)
In the comment I made in your question
在我在你的问题中发表的评论中
public void setParkAcres(int parkAcres) {
if (parkAcres < 0 || parkAcres > 400) {
System.out
.println("You have entered an invalid acre amount, it has been defaulted to 0");
parkAcres = 0;
}
this.parkAcres = parkAcres;
}
Is not very useful because whether or not the if
condition evaluates to true
, this line
不是很有用,因为无论if
条件是否计算为true
,这一行
this.parkAcres = parkAcres;
will get executed. You need to return
inside the if
.
将被执行。您需要return
在if
.
public void setParkAcres(int parkAcres) {
if (parkAcres < 0 || parkAcres > 400) {
System.out
.println("You have entered an invalid acre amount, it has been defaulted to 0");
parkAcres = 0;
return;
}
this.parkAcres = parkAcres;
}
The return;
statement will stop execution of the method where it is and return execution to the caller of the method. This is unrelated to your other problem.
该return;
语句将停止执行它所在的方法并将执行返回给方法的调用者。这与您的其他问题无关。
回答by Amr Gawish
Try
尝试
facilityCheck = request();