我想让这个程序更短。请推荐

问题描述 投票:0回答:1

我想编写这个程序来使用边或角度输入检查三角形的类型。我尝试了下面的代码,它在逻辑和结果方面都运行得很好。但我觉得它是多余的,可以用更少的方法来缩短。

所以我想知道这个程序是否可以更短?


import java.util.Scanner;
public class Test123 {
    int a;
    int b;
    int c;
    public Test123(int a, int b, int c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }
    public void determineType() {
        if(a >= (b+c) || c >= (b+a) || b >= (a+c) ) {
            System.out.println( "Not a Triangle");
        } else if(a==b && b==c) {
            System.out.println( "Equilateral Triangle");
        } else if (((a * a) + (b * b)) == (c * c) || ((a * a) + (c * c)) == (b * b) || ((c * c) + (b * b)) == (a * a)) {
            System.out.println( "Right Triangle");
        } else if(a!=b && b!=c && c!=a) {
            System.out.println( "Scalene Triangle" );
        } else if ((a==b && b!=c ) || (a!=b && c==a) || (c==b && c!=a)) {
            System.out.println( "Isosceles Triangle");
        }
    }
    public void determineTypeA() {
        if(a+b+c!=180) {
            System.out.println( "Not a Triangle");
        } else if(a==b && b==c) {
            System.out.println( "Equilateral Triangle");
        } else if (a==90||b==90||c==90) {
            System.out.println( "Right Triangle");
        } else if(a!=b && b!=c && c!=a) {
            System.out.println( "Scalene Triangle" );
        } else if ((a==b && b!=c ) || (a!=b && c==a) || (c==b && c!=a)) {
            System.out.println( "Isosceles Triangle");
        }
    }
    
    public static void calc() {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        int b = in.nextInt();
        int c = in.nextInt();
        Test123 t = new Test123(a, b, c);
        t.determineType();
    }
    public static void calc2() {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        int b = in.nextInt();
        int c = in.nextInt();
        Test123 t = new Test123(a, b, c);
        t.determineTypeA();
    }
    public static void main(String [] args) {
        char choice;
        Scanner in = new Scanner(System.in);
        System.out.println("Enter 'a' for angle input and 's' for sides input");
        choice=in.next().charAt(0);
        switch(choice)
        {
            case 'a'
            :System.out.println("Enter the angles of Triangle");
            calc2();       
            break;
            case 's'
            :System.out.println("Enter the sides of Triangle");
            calc();
            break;
        }
    }
}

请帮忙。

java class methods switch-statement triangle
1个回答
0
投票

所以,我假设您已经发现这里有重复的代码:

Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
Test123 t = new Test123(a, b, c);

因此,您可以做的是将这段代码从

calc()
calc2()
提取到一个新方法中:

public static Test123 getTest123FromUserInput(){
    Scanner in = new Scanner(System.in);
    int a = in.nextInt();
    int b = in.nextInt();
    int c = in.nextInt();
    return new Test123(a, b, c);
}

并从

calc()
calc2()
调用它:

public static void calc2() {
    Test123 t = getTest123FromUserInput();
    t.determineTypeA();
}

当然,你可以进一步减少它,通过将

calc()
calc2()
移动到
switch
-case:

switch(choice)
        {
            case 'a':
                System.out.println("Enter the angles of Triangle");
                getTest123FromUserInput().determineTypeA();
                break;
            case 's':
                System.out.println("Enter the sides of Triangle");
                getTest123FromUserInput().determineType();
                break;
        }

但这可能会减少太多,使得以后扩展变得更加困难。但它“更短”。

© www.soinside.com 2019 - 2024. All rights reserved.