Java 第三周的编程,只是寻找提示

Java 第三周的编程,只是寻找提示,java,performance,Java,Performance,我在学校学习Java的第三周,我刚刚提交了一份工资计算申请。作为一个极端的新手,我知道我的代码可能很可怕,会让一个经验丰富的程序员头疼——但这就是我来这里的原因。这项任务已经交上来了,所以我不想找人帮我做工作,我只是想知道我的代码中有一些具体的东西,以及一些一般的“你应该这样做”反馈,如果有人愿意帮助一个有3周经验的人变得更好的话 首先,这是我的代码: // Payroll.java // This program calculates an employee's net pay // minu

我在学校学习Java的第三周,我刚刚提交了一份工资计算申请。作为一个极端的新手,我知道我的代码可能很可怕,会让一个经验丰富的程序员头疼——但这就是我来这里的原因。这项任务已经交上来了,所以我不想找人帮我做工作,我只是想知道我的代码中有一些具体的东西,以及一些一般的“你应该这样做”反馈,如果有人愿意帮助一个有3周经验的人变得更好的话

首先,这是我的代码:

// Payroll.java
// This program calculates an employee's net pay
// minus Federal tax (11% off the gross) with Overtime
// Daniel Zaleski
// 08/17/2014

/** Import Scanner */

import java.util.Scanner; // Scanner to read user input

/** Create Class */

public class Payroll // Class declaration
{

    public static double hoursWorked; // Declare hoursWorked as a double

    public double hoursWorked()
    {
        return hoursWorked;
    }

    public static double hourlyRate; // Declare hourlyRate as a double

    public double hourlyRate()
    {
        return hourlyRate;
    }

    private double taxedPay; // Declare taxedPay as a double

    public double taxedPay()
    {
        return taxedPay;
    }

    private double netPay; // Declare netPay as a double

    public double netPay()
    {
        return netPay;
    }

    private static double grossPay; // Defining variable grossPay
    private static double overtimePay; // Defining variable overtimePay
    private static double overtimeHours; // Defining variable overtimeHours

    public static void main(String[] args) // Main method
    {

        int i = 8; // Setting the application loop
        while(i < 9) // Setting the termination variable which doesn't exist to continue loop
        {

            Scanner input = new Scanner(System.in); // Creating the scanner

            /** Prompt for first name */
            String firstName; // Variable for the first name
            System.out.println("\nEnter Your First Name: (Enter the name quit (all lowercase) to terminate application)\n");
            firstName = input.next();

            // Setting the loop termination variable "quit"
            if(firstName.equals("quit"))
            {
                System.out.println("You have terminated the Application. Have a nice day.\n");
                break;
            }

            /** Prompt for last name */
            String lastName; // Variable for the last name
            System.out.println("\nEnter Your Last Name:\n ");
            lastName = input.next();

            String completeName; // Setting the full name
            completeName = firstName + " " + lastName;
            System.out.println("\nHello " + completeName);

            /** Prompt for hours worked */
            double hoursWorked; // Asking for hours worked
            System.out.println("\nPlease Enter Your Hours Worked\n ");
            hoursWorked = input.nextDouble();
            if(hoursWorked < 0.1)
            {
                System.out.print("Please Enter a Positive Value\n ");
                System.out.println("\nPlease Enter Your Hours Worked (Greater Than 0)\n ");
                hoursWorked = input.nextDouble();
            }

            /** Prompt for hourly rate */
            double hourlyRate; // Defining hourlyRate
            System.out.println("\nPlease Enter Your Hourly Rate\n ");
            hourlyRate = input.nextDouble();
            if(hourlyRate < 0.1)
            {
                System.out.print("Please Enter a Positive Value\n ");
                System.out.println("\nPlease Enter Your Hourly Rate (Greater Than 0)\n ");
                hourlyRate = input.nextDouble();
            }

            /** Figure Out Overtime Pay */
            if(hoursWorked > 40.1)
            {
                overtimePay = 1.5 * hourlyRate * (hoursWorked - 40.0); // Figuring overtimePay
            }

            if(hoursWorked > 40.1)
            {
                grossPay = hourlyRate * hoursWorked + overtimePay; // Figuring overtime gross
            }

            else
            {
                grossPay = hourlyRate * hoursWorked; // Non overtime gross
            }


            /** Figure Out Overtime Hours */

            if(hoursWorked > 40.1)
            {
                overtimeHours = hoursWorked - 40; // Figuring overtimeHours
            }

            double federalTax; // Defining the Federal Tax variable
            federalTax = .11;

            double taxedPay; // Defining the taxedPay variable
            taxedPay = federalTax * grossPay;

            double netPay; // Defining the netPay variable
            netPay = grossPay - taxedPay;

            /** System print declarations */
            System.out.println("\nHello " + completeName); // Print user name
            System.out.println("\nYour Hours Worked this Cycle Are: " + hoursWorked); // Print hours worked
            System.out.printf("\nYour Current Hourly Rate is: $%,.2f\n", hourlyRate); // Print and format hourlyRate

            if(hoursWorked > 40.0)
            {
                System.out.printf("\nYour Overtime Hours This Cycle: %.2f\n", overtimeHours); // Print and format hourlyRate
                System.out.printf("\nYour Overtime Pay This Cycle: $%,.2f\n", overtimePay); // Print and format hourlyRate
            }

            System.out.printf("\nYour Gross Pay this Cycle is: $%,.2f\n", grossPay); // Print and format grossPay
            System.out.printf("\nThe Federal Tax you Paid this Cycle is: $%,.2f\n", taxedPay); // Print and format taxedPay
            System.out.println("\nYour Federal Tax Rate is Currently: (11%)"); // Print Federal tax rate
            System.out.printf("\nYour Net Pay for the Cycle is: $%,.2f\n", netPay); // Print and format netPay
            System.out.println("\nThe word quit was not detected. Let's do it again!\n"); // Create space to do the loop again

        } // End loop while
    } // End main method
} // End Payroll class     
//Payroll.java
//该程序计算员工的净工资
//减去联邦税(总收入的11%)和加班费
//丹尼尔·扎莱斯基
// 08/17/2014
/**导入扫描仪*/
导入java.util.Scanner;//扫描仪读取用户输入
/**创建类*/
公共类工资单//类声明
{
公共静态双小时工作;//将小时工作声明为双小时
公共双小时工作()
{
返回工作小时数;
}
public static double hourlyRate;//将hourlyRate声明为双精度
公共双小时
{
返回时长;
}
私人双重征税日;//将双重征税日声明为双重征税日
公共双重征税日
{
退还税款;
}
private double netPay;//将netPay声明为double
公共双网付()
{
退回网付;
}
私有静态双grossPay;//定义变量grossPay
私有静态双overtimePay;//定义变量overtimePay
私有静态双overtimeHours;//定义变量overtimeHours
公共静态void main(字符串[]args)//main方法
{
int i=8;//设置应用程序循环
while(i<9)//设置不存在的终止变量以继续循环
{
扫描仪输入=新扫描仪(System.in);//创建扫描仪
/**提示输入名字*/
String firstName;//第一个名称的变量
System.out.println(“\n输入您的名字:(输入名字quit(全部小写)以终止应用程序)\n”);
firstName=input.next();
//设置循环终止变量“quit”
如果(firstName.equals(“退出”))
{
System.out.println(“您已经终止了应用程序。祝您愉快。\n”);
打破
}
/**提示输入姓氏*/
字符串lastName;//姓氏的变量
System.out.println(“\n输入您的姓氏:\n”);
lastName=input.next();
String completeName;//设置全名
completeName=firstName+“”+lastName;
System.out.println(“\nHello”+completeName);
/**工作时间提示*/
工作两小时;//要求工作几个小时
System.out.println(“\n请输入您的工作时间\n”);
hoursWorked=input.nextDouble();
如果(工作小时<0.1)
{
System.out.print(“请输入一个正值\n”);
System.out.println(“\n请输入您的工作时间(大于0)\n”);
hoursWorked=input.nextDouble();
}
/**提示按小时收费*/
double hourlyRate;//定义hourlyRate
System.out.println(“\n请输入您的小时费率\n”);
hourlyRate=input.nextDouble();
如果(小时速率<0.1)
{
System.out.print(“请输入一个正值\n”);
System.out.println(“\n请输入您的小时费率(大于0)\n”);
hourlyRate=input.nextDouble();
}
/**计算加班费*/
如果(工作小时数>40.1)
{
overtimePay=1.5*hourlyRate*(工作小时数-40.0);//计算overtimePay
}
如果(工作小时数>40.1)
{
grossPay=hourlyRate*工时+加班工资;//计算加班总工资
}
其他的
{
grossPay=hourlyRate*工时;//非加班总工时
}
/**计算加班时间*/
如果(工作小时数>40.1)
{
overtimeHours=工作小时数-40;//计算超时小时数
}
double federalTax;//定义联邦税变量
联邦税=0.11;
双重taxedPay;//定义taxedPay变量
缴税日=联邦税*总税;
double netPay;//定义netPay变量
净支付=总支付-缴税日;
/**系统打印声明*/
System.out.println(“\nHello”+completeName);//打印用户名
System.out.println(“\n此周期的工作小时数为:“+hoursWorked”);//打印工作小时数
System.out.printf(“\n您当前的小时费率为:$%,.2f\n”,hourlyRate);//打印并格式化hourlyRate
如果(工作小时数>40.0)
{
System.out.printf(“\n此周期的加班时间:%.2f\n”,超时时间);//打印并格式化hourlyRate
System.out.printf(“\n本周期加班费:$%,.2f\n”,加班费);//打印并格式化hourlyRate
}
System.out.printf(“\n本周期的总工资为:$%,.2f\n”,grossPay);//打印并格式化grossPay
System.out.printf(“\n您在本周期支付的联邦税为:$%,.2f\n”,taxedPay);//打印并格式化taxedPay
System.out.println(“\n您的联邦税率当前为:(11%)”;//打印联邦税率
System.out.printf(“\n您在该周期的净支付额为:$%,.2f\n”,netPay);//打印并格式化netPay
System.out.println(“\n未检测到单词quit。让我们再试一次!\n”
            while (true) {
                if (hoursWorked < 0.1) {
                    System.out.print("Please Enter a Positive Value\n ");
                    System.out
                            .println("\nPlease Enter Your Hours Worked (Greater Than 0)\n ");
                    hoursWorked = input.nextDouble();
                } else 
                    break;
            }
while(true)
{
    //your code
   if(firstName.equals("quit"))
    {
        System.out.println("You have terminated the Application. Have a nice day.\n");
        break;
    }
}
 /** Figure Out Overtime Hours and Pay */
  if(hoursWorked > 40.1)
    {
        overtimeHours = hoursWorked - 40; // Figuring overtimeHours
        overtimePay = 1.5 * hourlyRate *overtimeHours ; // Figuring overtimePay
        grossPay = hourlyRate * hoursWorked + overtimePay; // Figuring overtime gross

    }
    else
    {
        overtimeHours=0;
        overtimePay=0;
        grossPay = hourlyRate * hoursWorked; // Non overtime gross
    }
public static double hoursWorked; // Declare hoursWorked as a double
public double hoursWorked() // this is the getter mothod of hoursWorked
{
    return hoursWorked;
}
public double getHoursWorked() // this is the getter mothod of hoursWorked
{
    return hoursWorked;
}
public class Payroll
{
    public double hoursWorked; // should not be static

    public double hoursWorked() //should be getHoursWorked() and should have setter
    {
        return hoursWorked;
    }

    public Payroll setHoursWorked(double hoursWorked)
    {
        this.hoursWorked = hoursWorked;
        return this; //returning itself instead of 'void' allows method chaining
    }

    public double hourlyRate; // should not be static

    public double hourlyRate() //should be getHourlyRate() and should have setter
    {
        return hourlyRate;
    }

    private double taxedPay; // Declare taxedPay as a double

    public double taxedPay() //should be getTaxedPay() and should have setter
    {
        return taxedPay;
    }

    private double netPay; 

    public double netPay() //should be getNetPay() and should have setter
    {
        return netPay;
    }

    private double grossPay; // should not be static, should have getter/setter
    private double overtimePay; // should not be static, should have getter/setter
    private double overtimeHours; // should not be static, should have getter/setter

    public static void main(String[] args)
    {
        int i = 8; //you are basically using this variable only as a substitute for "while(true)".
        Scanner input = null;
        try
        {
            input = new Scanner(System.in); // should not re-allocate the Scanner each call, it is a resource that is ought to be closed
            do // should just use a do-while loop
            {    
                String firstName;
                System.out.println("\nEnter Your First Name: (Enter the name quit (all lowercase)     to terminate application)\n");
                firstName = input.next();

                if("quit".equals(firstName)) //this way, the app doesn't crash with NullPointerException if firstName is null
                {
                    System.out.println("You have terminated the Application. Have a nice day.\n");
                    break;
                }

                String lastName;
                System.out.println("\nEnter Your Last Name:\n ");
                lastName = input.next();

                String completeName;
                completeName = firstName + " " + lastName;
                System.out.println("\nHello " + completeName);

                PayRoll payRoll = new PayRoll(); //have an actual class for the data instead of using it statically
                //...
                payRoll.setHoursWorked(input.nextDouble()); //use setters like that, also there is no exception handling in case it's not a double from the input
                if(payRoll.getHoursWorked() < 0.1)
                {
                    //...
                }

                // ...
                // print print print print etc
            } 
            while(i < 9); //to be honest, this is not very descriptive, a boolean variable would say more about what is happening here and up to what point it is supposed to run and when this loop should end
        }
        finally
        {
            if(input != null)
            {
                input.close(); // Scanner is a resource that is meant to be closed after usage
            }
        }
    }
}
public double getHoursWorked() {
    return this.hoursWorked;
}
public double calculateOvertimeHours() {
    //do your calculation here
}
// Payroll.java
// This program calculates an employee's net pay
// minus Federal tax (11% off the gross) with Overtime
// Daniel Zaleski
// 08/17/2014

/** Import Scanner */

import java.util.Scanner; // Scanner to read user input

/** Create Class */

public class Payroll // Class declaration
{
    private static final Double NORMAL_WORKING_HOURS = 40d;
    private static final Double SOME_FACTOR = 1.5;
    private static final Double FEDERAL_TAX = 0.11;

    private Scanner input;

    private String firstName;
    private String lastName;
    private String completeName;

    private double hoursWorked; // Declare hoursWorked as a double
    private double hourlyRate; // Declare hourlyRate as a double
    private double taxedPay; // Declare taxedPay as a double
    private double grossPay; // Defining variable grossPay
    private double netPay; // Declare netPay as a double
    private double overtimePay; // Defining variable overtimePay
    private double overtimeHours; // Defining variable overtimeHours

    public Payroll(Scanner input)
    {
        this.input = input;
    }

    public void run()
    {
        while(true)
        {
            inputFirstName();
            if (firstName.equals("quit"))
            {
                break;
            }
            inputLastName();
            createUser();
            welcomeUser();

            inputHoursWorked();
            inputHourlyRate();
            calculateOvertimeHours();
            calculateOvertimePay();
            calculateGrossPay();
            calculateTaxPay();
            calculateNetPay();
            printDeclaration();
        }

        System.out.println("You have terminated the Application. Have a nice day.\n");
    }

    private void inputFirstName()
    {
        /** Prompt for first name */
        System.out.println("\nEnter Your First Name: (Enter the name quit (all lowercase) to terminate application)\n");
        firstName = input.next();
    }

    private void inputLastName()
    {
        /** Prompt for first name */
        System.out.println("\nEnter Your Last Name\n");
        lastName = input.next();
    }

    private void createUser()
    {
        completeName = firstName + " " + lastName;
    }

    private void welcomeUser()
    {

        System.out.println("\nHello " + completeName);
    }

    private void inputHoursWorked()
    {
        System.out.println("\nPlease Enter Your Hours Worked\n ");
        hoursWorked = input.nextDouble();

        while(hoursWorked <= 0)
        {
            System.out.print("Please Enter a Positive Value\n ");
            hoursWorked = input.nextDouble();
        }
    }

    private void inputHourlyRate()
    {
        System.out.println("\nPlease Enter Your Hourly Rate\n ");
        hourlyRate = input.nextDouble();

        while(hoursWorked <= 0)
        {
            System.out.println("\nPlease Enter Your Hourly Rate (Greater Than 0)\n ");
            hourlyRate = input.nextDouble();
        }
    }


    private void calculateOvertimeHours()
    {
        if(hoursWorked > NORMAL_WORKING_HOURS)
        {
            overtimeHours = hoursWorked - NORMAL_WORKING_HOURS; // Figuring overtimeHours
        }
    }

    private void calculateOvertimePay()
    {
        overtimePay = SOME_FACTOR * hourlyRate * (overtimeHours); // Figuring overtimePay
    }

    private void calculateTaxPay()
    {
        taxedPay = FEDERAL_TAX * grossPay;
    }

    private void calculateGrossPay()
    {
        /** Figure Out Overtime Pay */
        if(overtimeHours > 0)
        {
            grossPay = hourlyRate * hoursWorked + overtimePay; // Figuring overtime gross
        }
        else
        {
            grossPay = hourlyRate * hoursWorked; // Non overtime gross
        }
    }

    private void calculateNetPay()
    {
        netPay = grossPay - taxedPay;
    }

    private void printDeclaration()
    {
        /** System print declarations */
        System.out.println("\nHello " + completeName); // Print user name
        System.out.println("\nYour Hours Worked this Cycle Are: " + hoursWorked); // Print hours worked
        System.out.printf("\nYour Current Hourly Rate is: $%,.2f\n", hourlyRate); // Print and format hourlyRate

        if(overtimeHours > 0)
        {
            System.out.printf("\nYour Overtime Hours This Cycle: %.2f\n", overtimeHours); // Print and format hourlyRate
            System.out.printf("\nYour Overtime Pay This Cycle: $%,.2f\n", overtimePay); // Print and format hourlyRate
        }

        System.out.printf("\nYour Gross Pay this Cycle is: $%,.2f\n", grossPay); // Print and format grossPay
        System.out.printf("\nThe Federal Tax you Paid this Cycle is: $%,.2f\n", taxedPay); // Print and format taxedPay
        System.out.println("\nYour Federal Tax Rate is Currently: (11%)"); // Print Federal tax rate
        System.out.printf("\nYour Net Pay for the Cycle is: $%,.2f\n", netPay); // Print and format netPay
        System.out.println("\nThe word quit was not detected. Let's do it again!\n"); // Create space to do the loop again
    }

    public static void main(String[] args) // Main method
    {
        Scanner input = new Scanner(System.in);
        new Payroll(input).run();
    } // End main method
} // End Payroll class