简体   繁体   English

从 Java 中的方法返回值

[英]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("The triangle is Isosceles");
        }
            
        else{ 
           System.out.println("The triangle is Scalene");  
        }
            
        return; 
        }
  
}

Currently your method is declared to return void - ie no value at all.目前您的方法被声明为返回void - 即根本没有价值。 Simply change it to return String instead:只需将其更改为返回String

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.有关更多信息,请在您学习 Java 的书中查找有关“返回”的部分。

Note that in almost all cases, comparing double values for exact equality is a bad idea.请注意,几乎在所有情况下,比较double值是否完全相等都是一个坏主意。 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.另请注意,在代码的早期部分,无需声明double变量,然后向用户写出一条消息。 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对象,每个对象都从标准输入中读取:

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:您需要将方法的返回类型从void(无)更改为String,然后使用“return”返回结果:

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:然后,您可以从 main 方法中调用它以获取结果:

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.您可以通过将方法的返回类型从 void 更改为 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM