• Post Reply Bookmark Topic Watch Topic
  • New Topic

Variable not responding properly(not increasing)  RSS feed

 
Rok Premuz
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

First of all, I would like to say Hi to everyone since I'm new here

The second thing is, I have a little problem with my code and because I'm still in the beginning stage of my Java learning I can't seem to make it work properly.
The problem is that I need the variable zasedenost to increase. For examle: 1=Pouring water in the container. It asks me how much water would i like to put in and I type 400. Now if i press 3 to do the loop again and decide to pour
the water out (case 2) and enter something, it reacts as if the container was empty, but i know it's not because I poured 400 units of water in it.
Thank you for your help.
Untitled.jpg
[Thumbnail for Untitled.jpg]
Main Class
Untitled2.jpg
[Thumbnail for Untitled2.jpg]
Methods class
 
Brian Burress
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java is pass by value, so the zasedenost variable declared in the main class is not changed as there is a local zasedenost variable in each of the methods (i.e. the parameter passed into the method). Within the method, the local zasedenost variable is being changed, but when you return out of the method then zasedenost is now scoped back to the main class declaration.

Based on the fact that the zasedenost variable in the methods are being returned, you should be able to update the main class to set (assign) the zasedenost in the main class to the value returned by the method. eg: zasedenost = VazaMetode.polnjenje(zasedenost);

I would suggest you not modify the passed parameter and return it, rather use a local temporary variable in the method to 'receive' the input zasedenost and then perform the calculations on it and then return it.
 
Noam Ingalls
Ranch Hand
Posts: 60
Chrome Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To OP: http://www.coderanch.com/how-to/java/PostTextNotScreenshots Would you kindly please use the code tags and copy the code in directly? The screenshots are difficult to read for those of us working on small screens and slow internet connections. Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!