[英]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.