Java方法,打印欢迎消息,然后是感谢信息

时间:2014-02-24 01:15:23

标签: java methods

整个代码非常简单,从之前的项目中我们或多或少地将其转换为使用方法。基本上它会提示用户输入他们的姓名,工作时间和工资率,获取该信息并计算净工资。我已经写了大部分代码,根据我的理解,它工作正常。

现在回答我的问题。一种方法必须在屏幕上打印一条消息,我必须在程序开始时调用该方法一次,打印一条欢迎消息,并在程序结束时打印一条感谢信息。有了这个,我迷失了如何制作它,所以一个方法可以确定它何时是程序的结束。 (当用户在提示输入其名称时输入-1时,程序将结束。)

package project.pkg2;

import java.util.*;

public class Project2 {

// Scanner for the console inputs
static Scanner console = new Scanner (System.in);

public static void main(String[] args) {

    String name, formatNet;
    double hours, pay, netPay;

 // Prints the welcome message from the method.
    welcomeMessage();

 // Every initialized variable receives the return statements from their respected methods.        
    name  = getName();

    while (!(name.equals("-1")))
    {
    pay   = getPay ();
    hours = getHours ();
    netPay = calcNet(pay,hours);

 // Formats the net pay to be 2 decimals.   
    formatNet = String.format("%.2f", netPay);
    System.out.println(name + "'s net pay is $" + formatNet + " \n");}

// Method for the welcome message, a void because it returns no values.
}
static void welcomeMessage ()
{
    System.out.println("Welcome to the CIS 220 Payroll Calculator!\n");
}

// Method that prompts the user to enter their name, scans it, then returns it.
static String getName ()
{
    String name;
    System.out.println("Please enter the employee's name(Enter a -1 when finished): ");
    name = console.nextLine();
    return name;

}

//Method that prompts the user to enter their pay rate, scans it, then returns it.    
static double getPay()
{
    double payRate;
    System.out.println("Please enter the employee's pay rate: ");
    payRate = console.nextDouble();
    console.nextLine();
    return payRate;
}

//Method that prompts the user to enter their hours worked, scans it, then returns it.        
static double getHours ()
{
    double hours;
    System.out.println("Please enter the employee's hours worked:");
    hours = console.nextDouble();
    console.nextLine();
    return hours;
}

//Method that uses the pay rate, hours worked that the user has entered.
//determines if the user qualifies for overtime pay or not, then calculates the overall    pay
//followed by tax reduction, then returns the netpay value.
static double calcNet (double pay, double hours)
{
   double net, grossPay;
   String formatNet;

    if(hours > 40)
    {
        grossPay = (pay * hours) * 1.5;
    }
    else
    {
        grossPay = pay * hours;
    }
    net = grossPay - (grossPay * .15);
    return net;
}


}

2 个答案:

答案 0 :(得分:1)

你可以使你的printMessage方法(从welcomeMessage重命名)采用一个布尔参数来告诉方法它是否应该显示欢迎或感谢信息。

static void printMessage(final boolean isStarting) {
    if(isStarting) {
        // print the welcome message
        ...
    } else {
        // print the thank you message
        ...
    }
}

然后在程序开头使用true调用方法,最后使用false调用方法。

或者你可以有一个类变量:

private boolean hasPrintedWelcome = false;

printMessage方法将是:

static void printMessage(final boolean isStarting) {
    if(!hasPrintedWelcome) {
        // print the welcome message
        ...
        hasPrintedWelcome = true;
    } else {
        // print the thank you message
        ...
    }
}

第一次调用printMessage方法时,它将显示欢迎消息。然后第二次调用该方法,以及随后的任何时间,该方法将显示感谢信息。

答案 1 :(得分:0)

您的程序存在一个问题:在进入while循环之前,您要求员工的姓名,因此名称将只设置一次并且永远不会更改,因此你会有一个无限循环。

您需要做的是在循环结束时再调用getName(),以允许用户设置新名称,然后退出循环:

// Every initialized variable receives the return statements from their respected methods.        
name  = getName();

while (!(name.equals("-1")))
{
    pay   = getPay (); 
    hours = getHours ();
    netPay = calcNet(pay,hours);

    // Formats the net pay to be 2 decimals.   
    formatNet = String.format("%.2f", netPay);
    System.out.println(name + "'s net pay is $" + formatNet + " \n");

    // Ask for the employee's name again
    name  = getName();
}

// Call the method to show the exit message here, like that: exitMessage();

一旦你修复了它,你就可以在while循环之后调用显示退出消息的方法。

编辑:似乎我误解了这个问题,无限循环只是一个错误而不是实际问题,请参阅另一个答案。