我正在自学 Java 方法,但不确定为什么这不会根据用户输入输出 boolean 值 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;
}
}
最佳答案
用下面的代码替换主方法中的最后一行,
System.out.println(calling.returnBoolean());
这应该可以正常工作。
另外请将类方法重命名为Methods(与方法名称不同,类名称应以大写字母开头)。
关于java - 在创建我的 java 方法时找不到错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19343218/