繁体   English   中英

如何在 Java 中提高可读性

[英]How would I make the readability better in Java

我觉得嵌套到彼此的 while 循环的数量不太可读,可以简化,但我不确定如何简化它。 有人可以让我知道我必须做些什么来简化这一点。 我不确定如何将这些 while 循环转换为方法,因为我的变量在 main 方法中。 我对 Java 很陌生,因此我们将不胜感激。

import java.util.Scanner;

public class Payroll 
{
    public static void main(String[] args)
    {
        //init scanner
        Scanner reader = new Scanner(System.in);

        //var
        boolean repeat = true; //keeps or breaks loop
        String strHWage; //input for hourly wage
        double dblHWage; //clctd for hourly wage from strHWage
        String strHours; //input for hours worked
        double dblHours; //clctd for hours worked
        String strOverHours; //input for overtime hours
        double dblOverHours; //clctd for overtime hours
        double totalPay; //clctd total pay

        
        //dscrp of prgrm to usr
        System.out.println();
        System.out.println("Calculate your weekly pay");
        System.out.println("Entering \"exit\" at any type will bring you back to the manager");
        System.out.println();

        //setting repeat to true
        repeat = true;

        while (repeat == true)
        {
            //rqst input for hourly wage
            System.out.print("Enter hourly wage: $");
            strHWage = reader.next();
            System.out.println();

            outerloop:
            while (isNumeric(strHWage))
            {
                if (strHWage.equalsIgnoreCase("exit"))
                {
                    repeat = false;

                    //bring to manager
                    System.out.println("Program terminated, going to manager\n");
                    Manager.main(args);
                }
                else if (isNumeric(strHWage))
                {
                    //str -> dbl
                    dblHWage = Double.parseDouble(strHWage);
                    
                    //rqst hours worked
                    System.out.print("Enter hours worked: ");
                    strHours = reader.next();
                    System.out.println();

                    while (isNumeric(strHours)) 
                    {
                        if(strHours.equalsIgnoreCase("exit"))
                        {
                            repeat = false;

                            //bring to manager
                            System.out.println("Program terminated, going to manager\n");
                            Manager.main(args);
                        }
                        else if (isNumeric(strHours))
                        {
                            //str -> dbl
                            dblHours = Double.parseDouble(strHours);

                            //rqst overtime hours worked
                            System.out.print("Enter overtime hours worked: ");
                            strOverHours = reader.next();
                            System.out.println();
                            
                            while (isNumeric(strOverHours))
                            {
                                if (strOverHours.equalsIgnoreCase("exit"))
                                {
                                    repeat = false;

                                    //bring to manager
                                    System.out.println("Program terminated, going to manager\n");
                                    Manager.main(args);
                                }
                                else if (isNumeric(strOverHours))
                                {
                                    //str -> dbl
                                    dblOverHours = Double.parseDouble(strOverHours);

                                    //clctd $ earned
                                    totalPay = (dblHWage * dblHours) + ((dblHWage * 1.5) * dblOverHours);

                                    //prnt total $ earned
                                    System.out.println("your weekly pay is $" + String.format("%.2f", totalPay));
                                    System.out.println();

                                    
                                    break outerloop;
                                }
                            }
                            if (!isNumeric(strOverHours))
                            {
                                System.out.println("please enter an actual number.\n");
                            }
                        }
                    }
                    if (!isNumeric(strHours))
                    {
                        System.out.println("please enter an actual number.\n");
                    }
                }
            }
            if (!isNumeric(strHWage))
            {
                System.out.println("please enter an actual number.\n");
            }
        }
    }

    //FUNCITON TO CHECK STR NUMERIC BEFORE PARSING
    public static boolean isNumeric(String str)
    {
        try
        {
            Double.parseDouble(str); //parses string if string value is numerical
            return true;
        } 
        catch(NumberFormatException ex) //returns false and does not parse if string value is not numerical
        {
            return false;
        }
    }
}

您可以通过将代码分解为更小的函数来简化代码。 我已经对您的代码进行了一些重构,但这可以进一步改进:-

import java.util.Scanner;

public class Payroll {

    //setting repeat to true
    static Boolean repeat = true;

    public static void main(String[] args) {
        //init scanner
        Scanner reader = new Scanner(System.in);

        //dscrp of prgrm to usr
        System.out.println("\nCalculate your weekly pay");
        System.out.println("Entering \"exit\" at any type will bring you back to the manager\n");

        while (repeat) {
            //rqst input for hourly wage
            System.out.print("Enter hourly wage: $");
            String strHWage = reader.next();
            System.out.println();

            outerloop:
            if (isNumeric(strHWage)) {
                //str -> dbl
                double dblHWage = Double.parseDouble(strHWage);

                //rqst hours worked
                System.out.print("Enter hours worked: ");
                String strHours = reader.next();
                System.out.println();

                if (isNumeric(strHours)) {
                    //str -> dbl
                    double dblHours = Double.parseDouble(strHours);

                    //rqst overtime hours worked
                    System.out.print("Enter overtime hours worked: ");
                    String strOverHours = reader.next();
                    System.out.println();

                    if (isNumeric(strOverHours)) {
                        //str -> dbl
                        double dblOverHours = Double.parseDouble(strOverHours);

                        //clctd $ earned
                        double totalPay = (dblHWage * dblHours) + ((dblHWage * 1.5) * dblOverHours);

                        //prnt total $ earned
                        System.out.println("your weekly pay is $" + String.format("%.2f", totalPay));
                        System.out.println();

                        break outerloop;
                    } else {
                        validateInput(strOverHours, args);
                    }
                } else {
                    validateInput(strHours, args);
                }
            } else {
                validateInput(strHWage, args);
            }
        }
    }

    public static void validateInput(String input, String[] args) {
        if (input.equalsIgnoreCase("exit")) {
            repeat = false;

            //bring to manager
            System.out.println("Program terminated, going to manager\n");
            Manager.main(args);
        } else {
            System.out.println("please enter an actual number.\n");
        }
    }

    //FUNCITON TO CHECK STR NUMERIC BEFORE PARSING
    public static boolean isNumeric(String str) {
        try {
            Double.parseDouble(str); //parses string if string value is numerical
            return true;
        } catch (NumberFormatException ex) //returns false and does not parse if string value is not numerical
        {
            return false;
        }
    }
}

暂无
暂无

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

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