Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Help with ints

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with ints

    I declared a public int in one class, modified it in that class, then tried to call it in another class, but when I call it, none of the modifications that were made carry over.

    The variable:
    public int dmg = 5;

    Where it is modified:
    case 1: 
    						if (gold >= 2){
    						dmg += 10;
    						gold -= 2;
    						out.println("Thank you. Have a nice day!");
    						}
    						else{
    							out.println("You are too poor.");
    						}
    						decision = 0;
    						break;
    What this is doing is buying a weapon, then adding to the dmg int.

    Where it is being called:
    static town city = new town();
    public int dmg(){
    	return dmg;
     
     
     
    case 1: 
    				wolf1hp -= city.dmg();
    				out.println("You have dealt " + city.dmg() + " damage." );
    				if (wolf1hp <= 0){
    					out.println(" You killed the first wolf.");
    				}
    				playerhealth -= wolf2dmg;
    				out.println("Wolf 2 attacked you and you lost " + wolf2dmg + ". You now have " + playerhealth + " health.");
    				decision = 0;
    				break;
    this is a simple battle scenario, and when i run it, instead of saying 15 damage it just says 5.
    Last edited by MAK; June 26th, 2014 at 03:12 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with ints

    Can you post the program's output that shows what you are talking about?
    Try debugging the code by adding some println() statements to print out the value of the variable every time it is changed and used.

    The formatting of the posted code has too much indentation making it hard to read.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with ints

    The name is dmg.
    That is all that would happen for formatting, but ill try again.
    here is the output.

    Thank you. Have a nice day!
    1 gold
    25 damage

    That is what it says after an item is purchased.

    You have dealt 5 damage.

    that is what it says after attacking.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with ints

    Try debugging the code by adding some println() statements to print out the value of the variable every time it is changed and used.
    The print out should show you where the value changes from 25 to 5.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with ints

    I did this at the very end of the first class, then at the very beginning of the second one, and it changed from 25 to 5, but i cant tell what messed it up.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with ints

    Copy the printouts that show each change to the dmg variable and each time that same variable was referenced. There should be a line where the value made an unexpected change.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with ints

    Please post a runnable example that demonstrates the problem. Also show what you'd like the results to be.

    Please follow Java's naming conventions. Class names begin with capital letters, variable and method names begin with lowercase letters.

  8. #8
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Help with ints

    You should post more code then what you did.
    Give us all of the code of both classes. Most likely you overwrote the reference to the variable at some point because of bad scopes.

Similar Threads

  1. Replies: 2
    Last Post: February 24th, 2014, 10:48 PM
  2. input.next and taking ints and doubles
    By CruelCoin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 6th, 2013, 07:00 AM
  3. Split a String into Ints
    By spunkyk014 in forum Java Theory & Questions
    Replies: 2
    Last Post: April 7th, 2013, 09:35 PM
  4. Evaluating two ints at the same time
    By Sean448 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 22nd, 2012, 05:21 AM
  5. Adding doubles and ints.
    By SkyAphid in forum Java Theory & Questions
    Replies: 6
    Last Post: September 8th, 2011, 05:54 PM