This site requires JavaScript, please enable it in your browser!
Greenfoot back
MarkusB
MarkusB wrote ...

2024/11/28

boolean in method

MarkusB MarkusB

2024/11/28

#
public void sammleGestein() { if(gesteinVorhanden()) { wertAlt = wert; wert = wert + ((Gestein)getOneIntersectingObject(Gestein.class)).wertGestein(); wertDifferenz = wert - wertAlt; nachricht("Diese Ware ist " + wertDifferenz + " Taler wert"); boolean annahme; sammeln(annahme); Greenfoot.delay(1); removeTouching(Gestein.class); } else { nachricht("Hier ist kein Gestein"); } } public void sammeln(boolean annahme) I cannot get this section to work. It says that variable "annahme" is not initialised and that is because it is not. It is supposed to be specified by the player once the previous check "if(gesteinVorhanden())" has been finished. Only then should "public void sammeln(boolean annahme)" be executed and a true or false should be given. : ( And "boolean annahme; sammeln(annahme); are correctly preparing that part, no? : (
danpost danpost

2024/11/29

#
MarkusB wrote...
<< Code Omitted >> I cannot get this section to work. It says that variable "annahme" is not initialised and that is because it is not. It is supposed to be specified by the player once the previous check "if(gesteinVorhanden())" has been finished. Only then should "public void sammeln(boolean annahme)" be executed and a true or false should be given. : ( And "boolean annahme; sammeln(annahme); are correctly preparing that part, no? : (
As a primitive variable type -- boolean, it requires initialization within a method or constructor. As an instance field, it would automatically be initialized. You have two immediate options, (1) just initialize it with one value and if the other results, change it; or (2) use a Boolean type and initialize it to null; then, you can set it at the appropriate time (being able to check for null , if it could possibly result as such, as well as what value it might contain).
You need to login to post a reply.