How do I assign random values to objects?












0















I am creating a card game that requires the cards to have random attributes. So I created a card class:



public static class hero{
static String name;
static int strength;
static int intellect;
static int flight;
static int tech;
}


The user then enters the number of cards and an array of cards (objects) is created.



hero  cards = new hero[cardNumber];
for(int i=0;i<cardNumber;i++){ cards[i]=new hero();}


However, when I try to assign random values to the cards using a for loop every card's attribute end up having the same value, this is the code I used:



for(int i=0; i<cards.length; ++i)
{
cards[i].strength = rand.nextInt(25) + 1;
cards[i].intellect = rand.nextInt(25) + 1;
cards[i].flight = rand.nextInt(25) + 1;
cards[i].tech = rand.nextInt(25) + 1;
}


Eg.



cards[1].flight 


would return 7



cards[2].flight 


would return 7



I am pretty sure I am wrong and any help and guidance is highly appreciated










share|improve this question





























    0















    I am creating a card game that requires the cards to have random attributes. So I created a card class:



    public static class hero{
    static String name;
    static int strength;
    static int intellect;
    static int flight;
    static int tech;
    }


    The user then enters the number of cards and an array of cards (objects) is created.



    hero  cards = new hero[cardNumber];
    for(int i=0;i<cardNumber;i++){ cards[i]=new hero();}


    However, when I try to assign random values to the cards using a for loop every card's attribute end up having the same value, this is the code I used:



    for(int i=0; i<cards.length; ++i)
    {
    cards[i].strength = rand.nextInt(25) + 1;
    cards[i].intellect = rand.nextInt(25) + 1;
    cards[i].flight = rand.nextInt(25) + 1;
    cards[i].tech = rand.nextInt(25) + 1;
    }


    Eg.



    cards[1].flight 


    would return 7



    cards[2].flight 


    would return 7



    I am pretty sure I am wrong and any help and guidance is highly appreciated










    share|improve this question



























      0












      0








      0








      I am creating a card game that requires the cards to have random attributes. So I created a card class:



      public static class hero{
      static String name;
      static int strength;
      static int intellect;
      static int flight;
      static int tech;
      }


      The user then enters the number of cards and an array of cards (objects) is created.



      hero  cards = new hero[cardNumber];
      for(int i=0;i<cardNumber;i++){ cards[i]=new hero();}


      However, when I try to assign random values to the cards using a for loop every card's attribute end up having the same value, this is the code I used:



      for(int i=0; i<cards.length; ++i)
      {
      cards[i].strength = rand.nextInt(25) + 1;
      cards[i].intellect = rand.nextInt(25) + 1;
      cards[i].flight = rand.nextInt(25) + 1;
      cards[i].tech = rand.nextInt(25) + 1;
      }


      Eg.



      cards[1].flight 


      would return 7



      cards[2].flight 


      would return 7



      I am pretty sure I am wrong and any help and guidance is highly appreciated










      share|improve this question
















      I am creating a card game that requires the cards to have random attributes. So I created a card class:



      public static class hero{
      static String name;
      static int strength;
      static int intellect;
      static int flight;
      static int tech;
      }


      The user then enters the number of cards and an array of cards (objects) is created.



      hero  cards = new hero[cardNumber];
      for(int i=0;i<cardNumber;i++){ cards[i]=new hero();}


      However, when I try to assign random values to the cards using a for loop every card's attribute end up having the same value, this is the code I used:



      for(int i=0; i<cards.length; ++i)
      {
      cards[i].strength = rand.nextInt(25) + 1;
      cards[i].intellect = rand.nextInt(25) + 1;
      cards[i].flight = rand.nextInt(25) + 1;
      cards[i].tech = rand.nextInt(25) + 1;
      }


      Eg.



      cards[1].flight 


      would return 7



      cards[2].flight 


      would return 7



      I am pretty sure I am wrong and any help and guidance is highly appreciated







      java random






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 23:05









      Peter O.

      20.9k95969




      20.9k95969










      asked Nov 14 '18 at 21:03









      Jeff WangJeff Wang

      62




      62
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Try removing the static keyword when you declare you variables in your hero class.
          So instead of



          static String name;
          ...
          static int flight;


          do



          String name;
          ...
          int flight;


          Making them static makes it so the variable is not specific to a single instance of the class, but rather the class as a whole. Static variables can be called upon without instantiating an object of the class.



          For example, with your current code



          hero.flight


          would also return 7. By removing the static identifier you will be able to make each of the variables specific to each object in your array.






          share|improve this answer
























          • Thank you very much, this has helped me a lot, initially, I got a null pointer exception error, then I realized that my loop parameters were not the same eg for the first loop for(int i=4;i<cardNumber;i++) and for the second loop for(int i=4;i<cardNumber;i++)

            – Jeff Wang
            Nov 17 '18 at 20:55













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308681%2fhow-do-i-assign-random-values-to-objects%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Try removing the static keyword when you declare you variables in your hero class.
          So instead of



          static String name;
          ...
          static int flight;


          do



          String name;
          ...
          int flight;


          Making them static makes it so the variable is not specific to a single instance of the class, but rather the class as a whole. Static variables can be called upon without instantiating an object of the class.



          For example, with your current code



          hero.flight


          would also return 7. By removing the static identifier you will be able to make each of the variables specific to each object in your array.






          share|improve this answer
























          • Thank you very much, this has helped me a lot, initially, I got a null pointer exception error, then I realized that my loop parameters were not the same eg for the first loop for(int i=4;i<cardNumber;i++) and for the second loop for(int i=4;i<cardNumber;i++)

            – Jeff Wang
            Nov 17 '18 at 20:55


















          0














          Try removing the static keyword when you declare you variables in your hero class.
          So instead of



          static String name;
          ...
          static int flight;


          do



          String name;
          ...
          int flight;


          Making them static makes it so the variable is not specific to a single instance of the class, but rather the class as a whole. Static variables can be called upon without instantiating an object of the class.



          For example, with your current code



          hero.flight


          would also return 7. By removing the static identifier you will be able to make each of the variables specific to each object in your array.






          share|improve this answer
























          • Thank you very much, this has helped me a lot, initially, I got a null pointer exception error, then I realized that my loop parameters were not the same eg for the first loop for(int i=4;i<cardNumber;i++) and for the second loop for(int i=4;i<cardNumber;i++)

            – Jeff Wang
            Nov 17 '18 at 20:55
















          0












          0








          0







          Try removing the static keyword when you declare you variables in your hero class.
          So instead of



          static String name;
          ...
          static int flight;


          do



          String name;
          ...
          int flight;


          Making them static makes it so the variable is not specific to a single instance of the class, but rather the class as a whole. Static variables can be called upon without instantiating an object of the class.



          For example, with your current code



          hero.flight


          would also return 7. By removing the static identifier you will be able to make each of the variables specific to each object in your array.






          share|improve this answer













          Try removing the static keyword when you declare you variables in your hero class.
          So instead of



          static String name;
          ...
          static int flight;


          do



          String name;
          ...
          int flight;


          Making them static makes it so the variable is not specific to a single instance of the class, but rather the class as a whole. Static variables can be called upon without instantiating an object of the class.



          For example, with your current code



          hero.flight


          would also return 7. By removing the static identifier you will be able to make each of the variables specific to each object in your array.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 21:19









          Sam LothamerSam Lothamer

          11




          11













          • Thank you very much, this has helped me a lot, initially, I got a null pointer exception error, then I realized that my loop parameters were not the same eg for the first loop for(int i=4;i<cardNumber;i++) and for the second loop for(int i=4;i<cardNumber;i++)

            – Jeff Wang
            Nov 17 '18 at 20:55





















          • Thank you very much, this has helped me a lot, initially, I got a null pointer exception error, then I realized that my loop parameters were not the same eg for the first loop for(int i=4;i<cardNumber;i++) and for the second loop for(int i=4;i<cardNumber;i++)

            – Jeff Wang
            Nov 17 '18 at 20:55



















          Thank you very much, this has helped me a lot, initially, I got a null pointer exception error, then I realized that my loop parameters were not the same eg for the first loop for(int i=4;i<cardNumber;i++) and for the second loop for(int i=4;i<cardNumber;i++)

          – Jeff Wang
          Nov 17 '18 at 20:55







          Thank you very much, this has helped me a lot, initially, I got a null pointer exception error, then I realized that my loop parameters were not the same eg for the first loop for(int i=4;i<cardNumber;i++) and for the second loop for(int i=4;i<cardNumber;i++)

          – Jeff Wang
          Nov 17 '18 at 20:55






















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308681%2fhow-do-i-assign-random-values-to-objects%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python