存储布尔值
我遇到的问题是如何更改给定类的布尔值,以便再次遇到它时它具有上次设置的值。这是我的类
public class Sandwich {
private String type;
private double price;
private String ing;
public boolean owned;
Sandwich (String t, double p, boolean o){
type = t;
price = p;
owned = o;
}
public boolean getO(){
return this.owned;
}
public void setO(boolean o){
this.owned = o;
}
public String getType(){
return this.type;
}
}
以及它被访问和应该更改的位置:
public void purchase(Sandwich s) {
boolean owned = s.owned;
//I tried also with accessor and mutator here but then changed to public
String type = s.getType();
if (owned == false) {
if (money <= 0){
System.out.println("Worker " + this.name + " can not buy " + type + " sandwich, cuz he doesn't have enough money");
} else {
System.out.println("Worker " + this.name + " can buy " + type + " sandwich");
this.money = money;
owned = true;
//this is the place where it is supposed to change value to true (sandwich was bought and has owner now
s.owned = owned;
}
} else if (owned == true) {
System.out.println("Worker " + this.name + " can not buy " + type + " sandwich cuz it was bought");
System.out.println("Test");
}
}
问题是,尽管过去购买了给定的三明治,但每次我尝试运行此代码时,其拥有的值都设置为 false。我需要三明治记录更改后的拥有值,以便下次运行时条件将是拥有== true。怎么可以
I have the problem how to change boolean value of a given class so that once it is encountered again it has the value last set. This is my class
public class Sandwich {
private String type;
private double price;
private String ing;
public boolean owned;
Sandwich (String t, double p, boolean o){
type = t;
price = p;
owned = o;
}
public boolean getO(){
return this.owned;
}
public void setO(boolean o){
this.owned = o;
}
public String getType(){
return this.type;
}
}
and place where it is accessed and supposed to change:
public void purchase(Sandwich s) {
boolean owned = s.owned;
//I tried also with accessor and mutator here but then changed to public
String type = s.getType();
if (owned == false) {
if (money <= 0){
System.out.println("Worker " + this.name + " can not buy " + type + " sandwich, cuz he doesn't have enough money");
} else {
System.out.println("Worker " + this.name + " can buy " + type + " sandwich");
this.money = money;
owned = true;
//this is the place where it is supposed to change value to true (sandwich was bought and has owner now
s.owned = owned;
}
} else if (owned == true) {
System.out.println("Worker " + this.name + " can not buy " + type + " sandwich cuz it was bought");
System.out.println("Test");
}
}
Problem is that although a given sandwich was bought in the past its owned value is set to false each time I try to run this code. I need for the sandwich to record changed value of owned so that next time I run the condition will be owned == true. How can it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的设计似乎存在缺陷。您需要在 Worker 和 sandwish 类型之间创建关系。
您可以做的只是在工人阶级中实现一个购买的三明治类型列表,并在工人购买三明治时与它进行比较。
或者,如果您愿意,您可以拥有所有三明治类型的哈希图,其中包含一个布尔值,指示该类型是否已被购买。
There seems to be a flaw in your design. You need to create a relationship between the Worker and the sandwish type.
What you can do is simply implement a List of purchased sandwish types in the worker class and compare against it whenever a worker purchases a sandwish.
Or if you want, you can have a hashmap of all sandwish types with a boolean value that indicates whether the type has already been purchased or not.
您创建了 get 和 set 例程,但没有使用它们。我会将代码更改为这样。
You created get and set routines and then didn't use them. I would change the code to this.
只需删除
您使用过
owned
并使用
s.getO()
,例如即可
并使用 setter 方法
s.setO(true/false)
更改它。例如
可以替换为
Just remove
and use
s.getO()
where you have usedowned
e.g.
can be
And use the setter method
s.setO(true/false)
to change it.e.g.
can be replaced by