问题描述
我目前在如何在代码上实现间接递归尾部,以便使其在输出完成时循环,而仅在输入-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 (将#修改为@)