Vswe's Summer Courses | You're not logged in. Sign up or log in | Server time: | Donate
One can now see other persons' badge hunts. Simply head over to their profiles to access them.

Go back to course page

Coding and a cup of Java

An introduction course to programming

Define multiple variables

  • 2013-09-05 11:30:21
    I know I can declare multiple variables in a single line, such as:
    int card1, card2, card3;

    Is there a way to declare and assign multiple variables to the same value in a single line?

    Something like:
    int card1, card2, card3 = 0;
  • 2013-09-05 11:40:12
    Hello pcWIZrd67!

    Yes, there is a way to do such a thing. You have 2 options, really:

    1. You can assign each variable to a value individually, in the same line

    int card1 = 0, card2 = 0, card3 = 0;

    2. Or you can assign the first variable, then the rest to the previous (in a pattern)

    int card1 = 0, card2 = card1, card3 = card2;

    Hope this helped, happy coding! :)
  • 2013-09-05 15:16:56
    Even though this is not what is being asked for I'm adding it for completeness. If you want to assign the same value to multiple variables which are already declared one can simply do:

    card1 = card2 = card3 = 0;
  • Log in or sign up to reply to this thread