简体   繁体   English

如何在 class 中将方法从第一种方法调用到第二种方法

[英]How to call method from 1st method to 2nd method in a class

The number is given and we need to find if it is a square number or triangle number?给定数字,我们需要找出它是平方数还是三角形数? This "num" should 1st verify the square method then it has to go for triangle method.这个“数字”应该首先验证平方方法,然后它必须 go 用于三角形方法。

there issue is: how can i call from square to triangle or any other methed to call the Triangle method if my square method won't suit.问题是:如果我的正方形方法不适合,我怎么能从正方形调用三角形或任何其他方法来调用三角形方法。

Here's my code:这是我的代码:

public class HelloWorld{

     public static void main(String []args){

        class Number{

            int num;

            public boolean isSquare(){

                int squareNumber=1;

                while(squareNumber<num){

                   squareNumber = num*num;

                }
                if(squareNumber%num==0)
                {
                    System.out.println(num+" is a Square number");
                }
                else
                {
                 return isTriangle();
                }

            boolean isTriangle() {

                int x=1,triangleNumber=1;

                while(triangleNumber<num){

                   x++;

                    triangleNumber = triangleNumber + x;

                }
                if(triangleNumber==num)
                {
                    System.out.println(num+" is a triangle number");
                }
                else
                {
                     System.out.println(num+" is applicable for both triangle and square numbers");
                }
            }
        }
    }
        Number mynum = new Number();
        mynum.num=2;
        System.out.println(mynum.isSquare());
    }
}

So first it would be much easier for you to just do this:所以首先,你这样做会容易得多:

public boolean isSquare(int num){
   ...
}

And so get rid of:所以摆脱:

mynum.num = yourNumber;

As very often you will encounter this approach in methods, where the raw input is can be any number/text...您经常会在方法中遇到这种方法,其中原始输入可以是任何数字/文本......

Now for your code, a very good optimization is:现在对于您的代码,一个非常好的优化是:

public boolean isSquare(int num){
   if (num>0) //To check if the number is null or not...
      squareNumber = num*num;
}

Because the while loop condition is always met once and then exited after the first entry...因为while循环条件总是满足一次,第一次进入后就退出了……

Moving on to the next part... which for me, is very tricky as I see it useless because it always returns true (the text) as the "squareNumber" always has num as root, and will always pass if (squareNumber%num==0) as true, and isTriangle(...) will never be called.继续下一部分......对我来说,这非常棘手,因为我认为它没用,因为它总是返回 true(文本),因为“squareNumber”总是以 num 作为根,并且总是通过 if (squareNumber%num ==0) 为真,并且永远不会调用 isTriangle(...) 。

The more approachable way is:更平易近人的方式是:

public class HelloWorld {

   static String result;

   public static void main(String[] args) {
       int mynum = 36;
       System.out.print(isSquare(mynum)+isTriangle(mynum));
     }

     public static String isSquare(int num){
       if ((Math.sqrt(num)==(int)Math.sqrt(num))){
         return "Given number is a square";
       } else return "Given number isn't a square,";
     }
     public static String isTriangle(int num){
      if (isSquare(8*num+1)){
        return " but is still a triangular number!";
      } else return " but not a triangular number.";
     }
  }

public static void main(String []args){公共 static 无效主(字符串 [] 参数){

        class Number{

            int num;

            public boolean isSquare(){

                int squareNumber=1;

                while(squareNumber<num){

                   squareNumber = num*num;

                }
                if(squareNumber%num==0)
                {
                    System.out.println(num+" is a Square number");
                }


                 return isTriangle();

            }
            boolean isTriangle() {

                int x=1,triangleNumber=1;

                while(triangleNumber<num){

                   x++;

                    triangleNumber = triangleNumber + x;

                }
                if(triangleNumber==num)
                {
                    System.out.println(num+" is a triangle number");
                    return true;
                }
                else
                {
                     System.out.println(num+" is applicable for both triangle and square numbers");

                }
                return false;

            }
        }


        Number mynum = new Number();
        mynum.num=2;
        System.out.println(mynum.isSquare());
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过从第1类调用第2类的方法来调用第3类的方法 - How to call a method of a 3rd class by calling the method of the 2nd class from the 1st class 尝试从第一类活动中调用方法,并在第二类活动中保留相同的参数(android studio) - Trying to call a method from 1st class activity and keep the same argument in a 2nd class activity (android studio) Java继承:第2个实例调用第1个实例方法 - Java Inheritance : 2nd instance calls 1st instance method Spring JPA 实体 findBy<Parameter> 在第一次尝试中给出值并在第二次调用中给出 null 的方法? - Spring JPA entity findBy<Parameter> method giving values in 1st attempt and null in 2nd call? 如何将代码从第二类添加到第一类 - How to add code from 2nd class to 1st class 使用android中的putextra和getextra方法从第二个活动转到第一个活动时图像视图的可见性消失了 - visibility gone of image view when come from 2nd activity to 1st activity using putextra and getextra method in android 如何从第二活动到第一活动获取数据 - How to get data from 2nd Activity to 1st Activity 从第一个活动拨打电话后,第二个活动没有通话 - 2nd Activity doesn't call after calling from 1st Activity 使用Enumeration <E>的hasMoreElements()和nextElement()方法打印列表,第1次成功,第2次添加失败后添加了一些元素 - using Enumeration<E> 's hasMoreElements() and nextElement() method to print a list, succeeded 1st time, failed 2nd time after adding some element 从Android中的第一个应用程序开始第二个应用程序的活动 - Start Activity of 2nd app from 1st app in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM