Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2006

Tip: Looking for answers? Try searching our database.

changing/saving a value in same variable

Thread view: 
quasi_dragon - 17 Mar 2006 19:52 GMT
Firstly, I'd like to show my source code.

-----Source Begin
class MonsterBattleExample
{
    public static void main(String args[])
    {
        int monsterHealth, toHit, swordDamage;
        monsterHealth = 25;
        toHit = DummiesRandom.getInt();
        swordDamage = DummiesRandom.getInt();
        {
            System.out.print("A monster stands before you with a current health
of ");
            System.out.print(monsterHealth);
            System.out.println(".");
            System.out.print("Your attack roll is ");
            System.out.print(toHit); //Reveals random to-hit
            System.out.println(".");
        if (toHit <= 8)
            {System.out.print("Your attack is successful! Your sword does ");
            System.out.print(swordDamage); //Reveals random damage
            System.out.println(" damage to the monster!");
            System.out.print("The monster's health is now ");
            System.out.print(monsterHealth - swordDamage);
            System.out.println(".");
            System.out.println();}
        else
            {System.out.println("Your attack failed! The monster laughs!");
            System.out.print("The monster's current health is still ");
            System.out.print(monsterHealth);
            System.out.println(".");
            System.out.println();}
        }
    }
}
-----Source End

This compiles and runs just fine (with, of course, the proper get
method). My problem is, adding more code to continue the battle. Note:
I am not concerned what happens if the monster reaches zero health, I
just want to get there with the current set of coding.

As you can see, I start with the value of 25 for the monster's health.
After the first attack, lets say I do 5 damage to the monster (damage
ranges from 1 to 10). So now the monster is at 20 health. But when I
try an add coding to attack the monster in its current 20 health, I end
up with damage still subtracting from the original value of 25 (rather
then saving a new value in the variable). How am I able to tell the JVM
that I want to truely continue damaging the monster from each
succeeding attack?

Before I continue with the rest of my lessons, I wanted to learn how to
send the same variable through a chain of events that would change the
value. Thanks for the help!
Monique Y. Mudama - 17 Mar 2006 20:06 GMT
> As you can see, I start with the value of 25 for the monster's health.
> After the first attack, lets say I do 5 damage to the monster (damage
[quoted text clipped - 4 lines]
> that I want to truely continue damaging the monster from each
> succeeding attack?

I'm not going to get into design issues ... I'll just answer the
question at hand.

You're just printing the damage; you're not saving it off anywhere.

Instead of

System.out.print(monsterHealth - swordDamage);

Do this:

monsterHealth -= swordDamage;
System.out.println(monsterHealth);

Now you've saved off the monster's current health so that you can refer
to it later.

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Oliver Wong - 17 Mar 2006 20:20 GMT
> Firstly, I'd like to show my source code.
>
[quoted text clipped - 51 lines]
> send the same variable through a chain of events that would change the
> value. Thanks for the help!

   You want to perform an assignment statement. See your line early on that
says "monsterHealth = 25;"? That's an example of an assignment statement.
"monsterHealth" is a variable, and you're assigning the value 25 to that
variable. Now let's say you want the new value of the monsterHealth to be
old value minus 10. So just assign exactly that:

<exampleCode>
monsterHealth = monsterHealth - 10;
</exampleCode>

   What if you want the new value to be the old value minus whatever value
is in "swordDamage"? You can specify exactly that as well:

<exampleCode>
monsterHealth = monsterHealth - swordDamage;
</exampleCode>

   - Oliver


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.