Java 方法,打印一条欢迎消息,然后打印一条感谢消息

标签 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;
}


}

最佳答案

您可以使 printMessage 方法(从welcomeMessage 重命名)采用一个 boolean 参数,告诉该方法是否应显示欢迎或感谢消息。

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 方法时,它将显示欢迎消息。然后第二次调用该方法,以及随后的任何一次,该方法都会显示感谢消息。

关于Java 方法,打印一条欢迎消息,然后打印一条感谢消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21977168/

相关文章:

java.io.StreamCorruptedException : invalid stream header: 4C756E69

python - 在 python 3 中的方法中更改全局字符串

java - JavaDoc 中@see 的用法?

java - 并发调用单例对象的Java方法

java - 使用 Marklogic 的 Java 搜索 API 与 XQuery/XSLT API 进行文档 XPath 搜索

java - 任务恢复InterruptionException但无法在Thread中捕获

java - 如何将 Realm 结果转换为数组并传递给另一个 Activity ?

java - 截断双数

java - 这两种方法有什么区别?它们看起来都一样,但每种方法都会导致不同的输出

C# 索引器的意义或好处是什么?