gpt4 book ai didi

java - 如何从不同的我返回 double 和字符串

转载 作者:行者123 更新时间:2023-12-01 23:06:53 25 4
gpt4 key购买 nike

我需要找出这三个数字是否构成一个三角形以及它是什么样的三角形。所以我有另一个 java 文件可以为我做到这一点。

package atriangle;

import java.util.Scanner;

public class TriangleTest
{
public static void main(String[] args)
{
double side1;
double side2;
double side3;
String typeOfTriangle = "";
Scanner input = new Scanner(System.in);
System.out.print("Side1: ");
side1 = input.nextDouble();
System.out.print("Side2: ");
side2 = input.nextDouble();
System.out.print("Side3: ");
side3 = input.nextDouble();
Triangle tester = new Triangle(side1, side2, side3, typeOfTriangle);
System.out.print("\nTriangle(" + side1 +"," + side2 + "," + side3 + ") " + typeOfTriangle);
}
}

测试人员去三角形java文件帮我回答上面的问题

package atriangle;

class Triangle
{
public Triangle(double sideA, double sideB, double sideC, String typeOfTriangle)
{
double bigside;
double leg;
double arm;

if(sideA>sideB)
{
if(sideA > sideC)
{
bigside = sideA;
leg = sideB;
arm = sideC;
if (isTriangle(arm, leg, bigside, typeOfTriangle)== true)
{
if (isRight(arm, leg, bigside, typeOfTriangle)== true)
{
return;//return is right
}
{
return;//return a triangle
}
}
else
{
sideA = 1.0;
sideB = 1.0;
sideC = 1.0;
return;//return not a triangle
}
}
else
{
bigside = sideC;
leg = sideB;
arm = sideA;
if (isTriangle(arm, leg, bigside, typeOfTriangle)== true)
{
if (isRight(arm, leg, bigside, typeOfTriangle)== true)
{
return;//return is right
}
{
return;//return a triangle
}
}
else
{
sideA = 1.0;
sideB = 1.0;
sideC = 1.0;
return;//return not a triangle
}
}
}
if (sideB > sideC)
{
bigside =sideB;
leg = sideA;
arm = sideC;
if (isTriangle(arm, leg, bigside, typeOfTriangle)== true)
{
if (isRight(arm, leg, bigside, typeOfTriangle)== true)
{
typeOfTriangle = "is right";
return;//return is right
}
{
return;//return a triangle
}
}
else
{
sideA = 1.0;
sideB = 1.0;
sideC = 1.0;
return;//return not a triangle
}
}
if(sideC > sideA)
{
bigside = sideC;
leg = sideB;
arm = sideA;
if (isTriangle(arm, leg, bigside, typeOfTriangle)== true)
{
if (isRight(arm, leg, bigside, typeOfTriangle)== true)
{
typeOfTriangle = "is right";
return;//return is right
}
{
return;//return a triangle
}
}
else
{
sideA = 1.0;
sideB = 1.0;
sideC = 1.0;
return;//return not a triangle
}
}
else
{
typeOfTriangle = "is equalaterial";
return;//return is equalaterial
}
}
public static boolean isTriangle(double arm, double leg, double bigside, String typeOfTriangle)
{
if (arm + leg > bigside)
{
return true;
}
{
return false;
}
}
public static boolean isRight(double leg, double arm, double bigside, String typeOfTriangle)
{
if (leg*leg + arm*arm == bigside*bigside)
{
return true;
}
{
return false;
}
}
}

但我不知道如何返回 String typeOfTriangle 的新值。谁能告诉我如何返回 String typeOfTriangle 和 double :sideA、sideB、sideC?

最佳答案

有很多方法可以做到这一点,但是使用您已有的代码,我将创建一个名为“TriangleType”的 Java 枚举,其中包含每种类型的三角形,然后我将向您的 Triangle 类添加一个静态方法当您传入三边作为参数时,返回枚举类型之一。然后,您可以向 Triangle 类添加一个名为“type”的类成员,该成员可以在构造函数中调用此方法,以便 Triangle 的每个实例都知道它是什么类型的三角形。这是对代码的快速修改,未经测试,因此可能存在语法错误。

public enum TriangleType {
NOT_A_TRIANGLE("Not a triangle"),
RIGHT("Right"),
EQUILATERAL("Equilateral"),
ISOSCELES("Isosceles"),
SCALENE("Scalene");

private String name;

TriangleType(String name) {
this.name = name;
}

public String toString() {
return this.name;
}
}

然后是 Triangle 类。

class Triangle
{
private int sideA;
private int sideB;
private int sideC;
private TriangleType type;

public Triangle(double sideA, double sideB, double sideC)
{
this.sideA = sideA;
this.sideB = sideB;
this.sideC = sideC;
this.type = Triangle::getTriangleType(sideA, sideB, sideC);
}

public TriangleType getType() {
return this.type;
}

public static TriangleType getTriangleType(double sideA, double sideB, double sideC) {

double bigside;
double leg;
double arm;

if(sideA>sideB)
{
if(sideA > sideC)
{
bigside = sideA;
leg = sideB;
arm = sideC;
if (isTriangle(arm, leg, bigside)== true)
{
if (isRight(arm, leg, bigside)== true)
{
return TriangleType.RIGHT;
}
}
else
{
return TriangleType.NOT_A_TRIANGLE;
}
}
else
{
bigside = sideC;
leg = sideB;
arm = sideA;
if (isTriangle(arm, leg, bigside)== true)
{
if (isRight(arm, leg, bigside)== true)
{
return TriangleType.RIGHT;
}
}
else
{
return TriangleType.NOT_A_TRIANGLE;
}
}
}
if (sideB > sideC)
{
bigside =sideB;
leg = sideA;
arm = sideC;
if (isTriangle(arm, leg, bigside)== true)
{
if (isRight(arm, leg, bigside)== true)
{
return TriangleType.RIGHT;
}
}
else
{
return TriangleType.NOT_A_TRIANGLE;
}
}
if(sideC > sideA)
{
bigside = sideC;
leg = sideB;
arm = sideA;
if (isTriangle(arm, leg, bigside)== true)
{
if (isRight(arm, leg, bigside)== true)
{
return TriangleType.RIGHT;
}
}
else
{
return TriangleType.NOT_A_TRIANGLE;
}
}
else
{
return TriangleType.EQUILATERAL;
}
}
public static boolean isTriangle(double arm, double leg, double bigside)
{
if (arm + leg > bigside)
{
return true;
}
{
return false;
}
}
public static boolean isRight(double leg, double arm, double bigside)
{
if (leg*leg + arm*arm == bigside*bigside)
{
return true;
}
{
return false;
}
}
}

以及你的主要方法。

public class TriangleTest
{
public static void main(String[] args)
{
double side1;
double side2;
double side3;

Scanner input = new Scanner(System.in);
System.out.print("Side1: ");
side1 = input.nextDouble();
System.out.print("Side2: ");
side2 = input.nextDouble();
System.out.print("Side3: ");
side3 = input.nextDouble();

Triangle tester = new Triangle(side1, side2, side3);

System.out.print("\nTriangle(" + side1 +"," + side2 + "," + side3 + ") " + tester.getType());
}
}

关于java - 如何从不同的我返回 double 和字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22598525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com