Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
陷入如何实现间接递归(java)的困境_Java_Loops_Recursion_Input_Tail Recursion - Fatal编程技术网

陷入如何实现间接递归(java)的困境

陷入如何实现间接递归(java)的困境,java,loops,recursion,input,tail-recursion,Java,Loops,Recursion,Input,Tail Recursion,我目前一直在研究如何在代码中实现一个间接递归尾部,以便它在输出完成时循环,并且只在输入-1时关闭 package triangle; class Triangle { double side1,side2,side3; public void getInput(double side1,double side2,double side3) { this.side1 = side1; this.side2 = side2

我目前一直在研究如何在代码中实现一个间接递归尾部,以便它在输出完成时循环,并且只在输入-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;
公共类三角形删除器
{
公共静态void main(字符串[]args)//main类
{
double side1、side2、side3;//初始化第1、2和3方。
扫描仪输入=新扫描仪(System.in);//允许用户输入
System.out.print(“输入三角形边的长度:”);
side1=input.nextDouble();
side2=input.nextDouble();
side3=input.nextDouble();
//用户将输入的三角形的三条边
if((checkValidity(side1,side2,side3))==1)//检查有效性。
{
三角形=新三角形();//初始化三角形
getInput(side1,side2,side3);//获取用户输入的输入
System.out.println(“三角形的周长是”+triangle.permiture());
//使用周长方法计算周长。
System.out.println(“三角形的面积是”+triangle.area());
//使用面积法计算面积
}else//如果值无效,程序将显示此消息。
{
System.out.println(“这些边没有指定有效的三角形”);
}
input.close();//关闭
}
//检查输入以查看值是否对三角形有效。
私有静态int checkValidity(双a、双b、双c){

如果(a+b),我无法想象递归对你的问题有多大帮助。你的老师要求使用递归吗?是的。他要求使用间接尾部递归来循环该项目。你可能想和他核实一下,以确保它是用于该项目的。除非你有三角形或其他什么的列表,否则我看不到任何地方(你不想对输入使用递归)这里可以使用递归偶数,除了输入,这会很奇怪。也许如果你有一个网格,它分解成一个三角形列表,那么可能。这是我实际上做的第一件事。他告诉我这个项目几乎就是他想要的。列表上的一个检查显示“使程序在没有正常if、else或while循环的情况下循环。”祝你好运。我看不到程序中有任何地方需要循环。除非你需要对更多三角形进行输入验证或循环。也许你可以给我们任务的完整描述?
import java.util.Scanner;

public class TriangleTester  
{
    public static void main(String[] args) //main class
    {
        double side1,side2,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, side2, side3))==1) //checks validity.
        {
            Triangle triangle = new Triangle(); //initializes triangle
            triangle.getInput(side1,side2,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;    
    }
}