这里有点丢失,似乎没有任何效果。我已经回读了有关返回方法的章节,我无法让它工作。
基本上我要创建一个接受园paremeter的方法,我有了为布尔值,野餐,游乐场,网球场和游泳池,这基本上得到声明为真,或者如果公园假getter和setter方法一类叫做公园拥有它们。如果公园有野餐和游乐场设施,它还需要返回一个布尔值。
这是我到目前为止所做的:
Park类:
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;
}
}
练习告诉我要为所有这些人制作吸气剂和制定者,我还没有使用其中一个。
这是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;
}
}
}
我不太了解练习要求我做什么,它要求我检查两个设施是否都可用并返回值。我所尝试的一切,价值每次都是假的。这本书在一章中教我这个,但实际上,它只显示了使用int / double变量而不是boolean,不知道这是否会产生影响,无论哪种方式我完全迷失了如何做返回方法... < / p>
通常我会只是提示他们想在公园里什么用户,然后使用制定者申报布尔值true,则使用干将if语句使用一个简单的displayParkInfo()方法将结果输出到控制台,看起来这个任务不希望我这样做。
答案 0 :(得分:2)
尝试
facilityCheck = request();
答案 1 :(得分:0)
对于引用类型,类字段默认初始化为null
,数字类型为0
,布尔类型为false
。你的领域
static boolean facilityCheck; // = false
将初始化为false
。因为你没有在任何地方重新初始化它所以它保持这种状态。您需要将其设置为request()
facilityCheck = request();
或让该方法本身设定其值。
if(userInput.nextInt() == 1){
ark1.setPicnicFacilities(true);
facilityCheck = true;
} else{
facilityCheck = false;
}
// return or not (change your method definition if not)
我在你的问题中发表的评论
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;
}
不是很有用,因为if
条件是否评估为true
,此行
this.parkAcres = parkAcres;
将被执行。您需要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;
}
return;
语句将停止执行该方法,并将执行返回给方法的调用者。这与你的其他问题无关。