在这个简单的程序中,有什么建议或教学可以提高效率吗?

6yoyoihd  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(309)

我创建了一个非常简单的程序,提示用户输入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.");
    }

}

gk7wooem

gk7wooem1#

是的,删除所有内容,然后替换为:

import java.util.Scanner;
//Do not use "import java.util.*;" this will import the entire library in java.util

public class Test1
{

    public static void main()
    {
        Scanner keyboard = new Scanner(System.in);
        int trianglePoint1X = keyboard.nextInt();
        int trianglePoint1Y = keyboard.nextInt();
        int trianglePoint2X = keyboard.nextInt();
        int trianglePoint2Y = keyboard.nextInt();
        int trianglePoint3X = keyboard.nextInt();
        int trianglePoint3Y = keyboard.nextInt();
        //No Arrays, they are unneccessary
        //No Checks, the triangle does not need to be checked to confirm it's a triangle, the user should have freedom to set points
        //Triangle should have it's corners stored in memory instead of it's sides
    }

    public void drawTriangle()
    {
        //connect triangle point 1 with 2
        //connect triangle point 2 with 3
        //connect triangle point 3 with 1   
    }
}

相关问题