需要帮助模拟小偷偷电视的程序

发布于 2025-01-06 23:10:38 字数 1696 浏览 0 评论 0原文

现在拿起一本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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅浅淡淡 2025-01-13 23:10:38

你的问题从根本上来说是这里

int nTVsStolen = 0;
int victimNumber = 0;
for(int i =0; i<victimNumber; i++)
{
 nTVsStolen = nTVsStolen + array[i];
}

第一个问题是代码无法编译,因为 array 是在 main 中定义的并且在此范围内不可见。下一个数组是 House 类型,因此不能与 + 运算符一起使用。

victimNumber 始终为零,因此循环永远不会执行。

也许将其替换为

while(nHDTVs > 0)
{
 nHDTVs--;
 nTVsStolen++;
 System.out.println("I stole a TV from this Victim: " + toString());
}

Your problem is fundamentally here

int nTVsStolen = 0;
int victimNumber = 0;
for(int i =0; i<victimNumber; i++)
{
 nTVsStolen = nTVsStolen + array[i];
}

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

while(nHDTVs > 0)
{
 nHDTVs--;
 nTVsStolen++;
 System.out.println("I stole a TV from this Victim: " + toString());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文