I am supposed to make a program that only has 2 methods. One method gets input from the user. The other method determines whether the triangle is Scalene, Isosceles or Equilateral. I have to then return the result back as a string to the original method (this is the part I am having trouble with). I don’t have a teacher I am just doing stuff from a booklet. Any help would be appreciated.
import java.util.Scanner;
public class Triangle {
public static void main (String args[]){
// Input for lengths
Scanner one = new Scanner(System.in);
double sideone;
System.out.println("Enter side one:");
sideone = one.nextDouble();
Scanner two = new Scanner(System.in);
double sidetwo;
System.out.println("Enter side two:");
sidetwo = two.nextDouble();
Scanner three = new Scanner(System.in);
double sidethree;
System.out.println("Enter side three:");
sidethree = three.nextDouble();
sides(sideone, sidetwo, sidethree);
}
static void sides(double sideone, double sidetwo, double sidethree){
if ((sideone == sidetwo) && (sideone == sidethree)){
System.out.println("The triangle is Equilateral");
}
if (((sideone == sidetwo) || (sideone == sidethree) || (sidetwo == sidethree)))
{
System.out.println("The triangle is Isosceles");
}
else{
System.out.println("The triangle is Scalene");
}
return;
}
}
You need to change the method’s return type from void (nothing) to String, and then use “return” to return the result:
You can then call it from you main method to get the result: