开发者

Returning value from method in Java

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(&qu开发者_如何学运维ot;The triangle is Isosceles");
        }
            
        else{ 
           System.out.println("The triangle is Scalene");  
        }
            
        return; 
        }
  
}


Currently your method is declared to return void - i.e. no value at all. Simply change it to return String instead:

static String sides(double side1, double side2, double side3) {
    if (side1 == side2 && side1 == side3){
        return "The triangle is Equilateral";
    }
    // etc
}

For further information, look for a section about "return" in the book you're learning Java from.

Note that in almost all cases, comparing double values for exact equality is a bad idea. It's probably okay in this particular case as you've just converted them from strings - but if you'd performed arithmetic on them, it would usually be better to compare them for equality within a certain tolerance.

Also note that in the early part of your code, there's no need to declare the double variable, then write out a message to the user. If you can combine declaration and assignment, it usually makes for clearer code. Likewise there's no need to create three different Scanner objects, each reading from standard input:

Scanner input = new Scanner(System.in);

System.out.println("Enter side one:");
double side1 = input.nextDouble();

System.out.println("Enter side two:");
double side2 = input.nextDouble();

System.out.println("Enter side three:");
double side3 = input.nextDouble();

(The common code here could be refactored even further, but this is a start...)


I have to then return the result back as a string to the original method (this is the part I am having trouble with).

You need to change the method's return type from void (nothing) to String, and then use "return" to return the result:

static String sides(double sideone, double sidetwo, double sidethree){

    if ((sideone == sidetwo) && (sideone == sidethree)){
         return "The triangle is Equilateral";
    }

You can then call it from you main method to get the result:

String theResult = sides(sideone, sidetwo, sidethree);


  1. You can return the String which you are printing by changing the return type of the method from void to String.
  2. Also you can improve the program by creating only one scanner object for reading all your inputs rather than creating one for each input like :

double sideone; double sidetwo; double sidethree;

Scanner one = new Scanner(System.in);

System.out.println("Enter side one:");
sideone = one.nextDouble();


System.out.println("Enter side two:");
sidetwo = one.nextDouble();


System.out.println("Enter side three:");
sidethree = one.nextDouble();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜