public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
int x = 20;
String choice = "";
while(x!=0) {
System.out.println("What would you like to do? Enter help for commands.");
choice = userInput.next().toLowerCase();
if(choice.equals("go right")) method();
if(choice.equals("go left")) method();
if(choice.equals("go forwards")) method();
if(choice.equals("go backwards")) method();
if(choice.equals("help")) System.out.println("I can print!");
x--;
System.out.println("Value of x: " + x);
}
}
Кажется, что приведенный выше код выполняется дважды, x уменьшается на два, прежде чем сканер снова запросит ввод данных пользователем.
public void method(){
System.out.println("methoding");
}
Кроме того, метод не запускается при вызове из оператора if, но оператор print будет.