我正在用 Java 自学方法,但不确定为什么这不会根据用户输入输出布尔值 true/false。任何帮助都是极好的。我对命名方法感到困惑,特别是当我想要 void/private 等时。谢谢!
import java.util.Scanner;
public class javaPractice
{
public static void main (String[]args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int x = input.nextInt();
methods calling = new methods(x);
calling.oddTest();
calling.returnBoolean();
}
}
public class methods
{
private int userInput;
private boolean output;
public methods (int num) //constructor
{
userInput = num;
}
public void oddTest ()
{
if (userInput % 2 == 0)
{
output = true;
}
else if (userInput % 2 != 0)
{
output = false;
}
}
public boolean returnBoolean ()
{
return output;
}
}