需要帮助模拟小偷偷电视的程序
现在拿起一本Java教科书。实际上再次尝试学习语言。
本书中有一段非常有趣的代码,它涉及一个名为 House 的类,它模拟小偷从人们的房子(也是类)偷电视。
唯一的问题是我似乎不知道如何修改我的数组来改变小偷收集的房屋数量。很难解释,但这是我现在的代码。我现在完全迷失了。我真的不明白数组是如何工作的,这让我很烦恼。任何帮助将不胜感激。
import java.util.Scanner;
class House {
public int nHDTVs;
public House pFriend;
public House(int nParameter, House friendParameter)
{
nHDTVs = nParameter;
pFriend = friendParameter;
}
public void steal(House thiefsHouse, House firstVictim)
{
int nTVsStolen = 0;
int victimNumber = 0;
for(int i =0; i<victimNumber; i++)
{
nTVsStolen = nTVsStolen + array[i];
}
//nTVsStolen = nTVsStolen +
/*for(i=1;i>10;i++)
{
//stuff
}*/
//thiefsHouse nHDTVs = n
System.out.println("Victim " + (victimNumber+1) + " " + "lives at " + firstVictim);
System.out.println("Victim " + (victimNumber+2) + " " + "lives at " + firstVictim.pFriend);
System.out.println("I've just stolen " + 0 + " TVs from the House at " + null);
thiefsHouse.nHDTVs = thiefsHouse.nHDTVs + nTVsStolen;
}
public static void main(String dfgasdfwe[]) {
int nHouses;
Scanner keyboard = new Scanner(System.in);
System.out.println("How many houses should the thief steal from?");
nHouses = keyboard.nextInt();
House array[] = new House[nHouses];
array[nHouses - 1] = new House(3, null);
for(int i = nHouses - 2; i>=0; i--)
{
array[i] = new House( i , array[i+1]);
}
House thiefsHouse = array[0];
//pFriend nHDTVs[victimNumber] = 0;
thiefsHouse.steal(thiefsHouse, thiefsHouse.pFriend);
System.out.println("I now have " + thiefsHouse.nHDTVs + " TV sets. HA HA.");
}
}
Picked up a Java Textbook now. Actually trying to learn the language again.
There is a very interesting piece of code in this book that entails a class called House that simulates a thief stealing TVs from people's houses (which are also classes).
Only problem is I can't seem to figure out how to modify my array to change the number of Houses that the thief collects. It's hard to explain but here is my code right now. I'm completely lost at the moment. I really can't understand how arrays work which is bugging the hell out of me. Any help would be appreciated.
import java.util.Scanner;
class House {
public int nHDTVs;
public House pFriend;
public House(int nParameter, House friendParameter)
{
nHDTVs = nParameter;
pFriend = friendParameter;
}
public void steal(House thiefsHouse, House firstVictim)
{
int nTVsStolen = 0;
int victimNumber = 0;
for(int i =0; i<victimNumber; i++)
{
nTVsStolen = nTVsStolen + array[i];
}
//nTVsStolen = nTVsStolen +
/*for(i=1;i>10;i++)
{
//stuff
}*/
//thiefsHouse nHDTVs = n
System.out.println("Victim " + (victimNumber+1) + " " + "lives at " + firstVictim);
System.out.println("Victim " + (victimNumber+2) + " " + "lives at " + firstVictim.pFriend);
System.out.println("I've just stolen " + 0 + " TVs from the House at " + null);
thiefsHouse.nHDTVs = thiefsHouse.nHDTVs + nTVsStolen;
}
public static void main(String dfgasdfwe[]) {
int nHouses;
Scanner keyboard = new Scanner(System.in);
System.out.println("How many houses should the thief steal from?");
nHouses = keyboard.nextInt();
House array[] = new House[nHouses];
array[nHouses - 1] = new House(3, null);
for(int i = nHouses - 2; i>=0; i--)
{
array[i] = new House( i , array[i+1]);
}
House thiefsHouse = array[0];
//pFriend nHDTVs[victimNumber] = 0;
thiefsHouse.steal(thiefsHouse, thiefsHouse.pFriend);
System.out.println("I now have " + thiefsHouse.nHDTVs + " TV sets. HA HA.");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的问题从根本上来说是这里
第一个问题是代码无法编译,因为
array
是在 main 中定义的并且在此范围内不可见。下一个数组是 House 类型,因此不能与 + 运算符一起使用。victimNumber
始终为零,因此循环永远不会执行。也许将其替换为
Your problem is fundamentally here
The first problem is that the code won't compile because
array
is defined in main and not visible in this scope. Next up array is of type House and so can't work with the + operator.victimNumber
is always zero and so the loop never executes.Maybe replace it with