我创建了一个非常简单的程序,提示用户输入3个整数,然后确定输入的变量是否创建了一个定义的三角形。为了提高效率,有没有什么提示或建议来简化这个过程?这是我为自己做的练习。我只有6个月的编码,还没有到效率,所以任何帮助或建议将是巨大的。谢谢您。
import java.util.*;
public class Test1
{
public static void main(String[] args)
{
int[] triangleInput = EnterSides();
DisplayArray(triangleInput);
DetermineValidTriangle(triangleInput);
}
public static int[] EnterSides()
{
Scanner keyboard = new Scanner(System.in);
int[] triangleInput = new int[3];
final int numOfSides = 3;
System.out.println("Please enter " + numOfSides + " integers: ");
for(int x = 0; x < triangleInput.length; ++x)
{
triangleInput[x] = keyboard.nextInt();
}
return triangleInput;
}
public static void DisplayArray(int[] userInput)
{
int[] triangleInput = userInput;
System.out.print("You have entered: " + Arrays.toString(triangleInput));
System.out.println();
System.out.println();
System.out.println("Side A of triangle is " + triangleInput[0] + ", Side B of triangle is " +
triangleInput[1] + ", and Side C of triangle is " + triangleInput[2]);
}
public static void DetermineValidTriangle(int[] userInput)
{
int[] triangleInput = userInput;
boolean isTriangle = false;
if(triangleInput[0] + triangleInput[1] > triangleInput[2])
if(triangleInput[1] + triangleInput[2] > triangleInput[0])
{
if(triangleInput[0] + triangleInput[2] > triangleInput[1])
{
isTriangle = true;
}
}
System.out.println();
if(isTriangle)
System.out.println("Successful triangle has been built!");
else
System.out.println("Sorry, Unsuccessful triangle. The requirement for a triangle is A + B
is greater than C. B + C is greater than A, and A + C is greater than B.");
}
}
1条答案
按热度按时间gk7wooem1#
是的,删除所有内容,然后替换为: