gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:21:40 26 4
gpt4 key购买 nike

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

现在回答我的问题。一种方法必须在屏幕上打印一条消息,我必须在程序开始时调用该方法一次,以打印欢迎消息,并在程序结束时调用该方法一次,以打印感谢消息。这样,我不知道如何制作它,以便用一个方法可以确定程序何时结束。 (当用户在提示输入姓名时输入 -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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com