微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

坚持如何实现间接递归java

如何解决坚持如何实现间接递归java

我目前在如何在代码上实现间接递归尾部,以便使其在输出完成时循环,而仅在输入-1时关闭,而陷入困境。

package triangle;
class Triangle {
    
    double side1,side2,side3;
    
    
    public void getInput(double side1,double side2,double side3) {
        this.side1 = side1;
        this.side2 = side2;
        this.side3 = side3;
    }
    // the function to calculate and return the area of the triangle.
    public double Area() 
    {
        double area = 0;
        double s = (side1 + side2 + side3) / 2; // calculate value of s.
        area = Math.sqrt(s * (s-side1) * (s-side2) * (s-side3));
        return area; // return area.
    }
    // the function to calculate and return perimeter of the triangle.
    public double Perimeter() 
    {  
        double perimeter = side1 + side2 + side3;
        return perimeter; // return perimeter of Triangle.
    }
}
import java.util.Scanner;

public class TriangleTester  
{
    public static void main(String[] args) //main class
    {
        double side1,side3; //initializes side 1,2,and 3.
        
        Scanner input = new Scanner(system.in); //allows the user to input
        
        System.out.print("Enter lengths of sides of the triangle: ");
        side1 = input.nextDouble();
        side2 = input.nextDouble();
        side3 = input.nextDouble();
        //the 3 sides of the triangle the user will input
        if ((checkValidity(side1,side3))==1) //checks validity.
        {
            Triangle triangle = new Triangle(); //initializes triangle
            triangle.getInput(side1,side3); //gets the input the user inputed  
            System.out.println("The perimeter of the triangle is "+triangle.Perimeter());
            // Calculates the perimeter with the Perimeter method.
            System.out.println("The area of the triangle is "+triangle.Area());
            //Calucates the area with the Area method
        } else //if the values are invalid program displays this message.
        {
            System.out.println("Those sides do not specify a valid triangle.");   
        }
        input.close(); //closes
    }
    //Checks the input to see if the values are valid for a triangle.
    private static int checkValidity(double a,double b,double c) { 
        if (a + b <= c || a + c <= b || b + c <= a)
        return 0;
        else
        return 1;    
    }
}

我假设递归将进入 Triangle 中的getInput()方法中,但是我不确定从那里继续。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。