0
whats wrong?
public class W{ static boolean pressing = false; public static void press(){ pressing = true; } public static void notPressing(){ pressing = false; } } public class Main { public static void main(String[] args) { W walk = new W(); var walking = walk.pressing (true); walk.press(); // press here if (walking){ System.out.println ("I'm walking!!!"); while (walking){ System.out.println ("still walking"); } }else{ System.out.println ("I'm not walking"); } } }
4 Réponses
+ 2
Change your variable pressing to private, then you can make a method that returns the value of pressing. Then move the method call to just before the if statement or use it as the condition for the statement.
In W class
private static boolean pressing = false;
public boolean isPressing() {
return pressing;
}
In main
var walking = walk.isPressing();
if(walking) {
...
...
Or delete walking variable and use
if(walk.isPressing()) {
...
...
+ 1
var walking = walk.pressing; // false
In your conditions for if and while you probably want walk.pressing but if you do so then you will have an infinite loop. It will run in the playground until it times out and then will print all the output.
+ 1
thank you!
0
yes i know... (just wanted to test it out). but how can i make this part right:
var walking = walk.pressing();
?