我目前正在进行课堂作业,无法弄清楚为什么我会得到我得到的输出。编程问题是:
你操作几个热狗架。定义名为
HotDogStand
的类 它有一个用于热狗机架的ID号的实例变量和一个 这个展位当天销售了多少热狗的实例变量。 创建一个允许该类用户初始化的构造函数 两个变量。还要创建一个名为justSold
的方法,该方法以 一个摊位已售出的热狗数量。这个想法是这个 每次看台卖热狗的时候都会调用方法 总计可以跟踪。添加另一个返回数字的方法 热狗卖了。添加一个静态变量,用于跟踪销售的热狗总数 所有看台和一个返回此值的静态方法 变量
所以我的代码是:
public class HotDogStand {
// instance variable declaration
private int IDNumber;
private int hotDogsSold = 0;
private static int totalSold = 0;
public HotDogStand(int ID, int sold) {
IDNumber = ID;
hotDogsSold = sold;
}
public int getID() {
return IDNumber;
}
public void setID(int ID) {
IDNumber = ID;
}
public void justSold() {
if (hotDogsSold > 0) {
hotDogsSold++;
}
}
public int sold() {
return hotDogsSold;
}
public static int getTotal() {
return totalSold;
}
}
我的测试课是:
public class HotDogTest {
public static void main(String[] args) {
HotDogStand stand1 = new HotDogStand(1, 11);
HotDogStand stand2 = new HotDogStand(2, 17);
HotDogStand stand3 = new HotDogStand(3, 6);
stand1.getID();
stand2.getID();
stand3.getID();
stand1.setID(1);
stand2.setID(2);
stand3.setID(3);
stand1.justSold();
stand2.justSold();
stand3.justSold();
stand1.justSold();
stand1.justSold();
stand1.justSold();
stand3.justSold();
stand1.getTotal();
stand2.getTotal();
stand3.getTotal();
int grandTotal = stand1.getTotal() + stand2.getTotal() + stand3.getTotal();
System.out.println("Stand " + stand1.getID() + " sold a total of " + stand1.getTotal() + " hotdogs.");
System.out.println("Stand " + stand2.getID() + " sold a total of " + stand2.getTotal() + " hotdogs.");
System.out.println("Stand " + stand3.getID() + " sold a total of " + stand3.getTotal() + " hotdogs.");
System.out.println("The total amount of hotdogs sold by all the stands was " + grandTotal);
}
}
我的输出是:
Stand 1共售出0个热狗 Stand 2共售出0个热狗 Stand 3共售出0个热狗 所有看台销售的热狗总数为0
答案 0 :(得分:1)
您永远不会更新totalSold字段。在justSold()方法的内部增加,如果条件。
答案 1 :(得分:0)
试试这个:
public void justSold() {
if (hotDogsSold > 0) {
totalSold = hotDogsSold++;
}
答案 2 :(得分:0)
没有必要更改import re,urllib2
def find_words(each_func):
i=0
wordsineach_func=[]
while len(each_func) >0:
i=i+1
word_found=longest_word(each_func)
if len(word_found)>0:
wordsineach_func.append(word_found)
each_func=each_func.replace(word_found,"")
# print i,word_found,each
return wordsineach_func
def longest_word(phrase):
phrase_length=len(phrase)
words_found=[];index=0
outerstring=""
while index < phrase_length:
outerstring=outerstring+phrase[index]
index=index+1
if outerstring in words or outerstring.lower() in words:
words_found.append(outerstring)
if len(words_found) ==0:
words_found.append(phrase)
return max(words_found, key=len)
data = urllib2.urlopen('https://s3.amazonaws.com/hr-testcases/479/assets/words.txt')
words=[]
for line in data:
words.append(line.replace("\n",""))
string="#honesthournow20"
string=string.replace("#","")
new_words=re.split(r'(\d+)',string)
output=[]
for each in new_words:
each_words=find_words(each)
for each_word in each_words:
output.append(each_word)
print output
变量。 totalSold
增加实例变量justsold
,但hotDogsSold
返回getTotal
。
您需要一个方法totalSold
,它返回实例变量getSold
。
另一个问题是hotDogsSold
是一个静态变量(它是一个类变量;它不依赖于类的任何单个实例,如热狗卖家1或2,而是整个热狗卖家模型)。因此,如果totalSold
正确递增,您的总计将为每个人提供3倍的热狗销售量。