java - 在创建我的 java 方法时找不到错误?

标签 java methods

我正在自学 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/

相关文章:

java - 包内包 Java

c++ - 如何指定方法更改了哪些数据成员?

Swift 实例属性与方法 : lowercased() vs capitalized

ASP.NET页面生命周期: methods vs Events

java - Java中内部类方法与实例方法的区别

java - java方法中int[]输入正确吗?

java - Maven 从两个单独的 pom.xml 部署两个具有不同分类器的 jar

java - Blowfish 加密问题

java - 将多个项目添加到 Java 中已初始化的数组列表

java - 我们可以多次发布流吗?