简体   繁体   English

Java方法,先显示欢迎消息,然后显示“谢谢”消息

[英]Java Method, print a welcome message, then a Thank you message

The whole code is pretty simple, from a previous project we more or less convert it to make use of methods. 整个代码非常简单,在上一个项目中,我们或多或少地将其转换为使用方法。 Basically it prompts the user for their name, hours worked, and pay rate, takes that info and calculates it for the net pay. 基本上,它会提示用户输入姓名,工作时间和薪水率,获取该信息并计算出净工资。 I've written up the majority of the code and from my understanding it works fine. 我已经编写了大部分代码,据我所知,它可以正常工作。

Now to my question. 现在我的问题。 One method must print a message to the screen, I have to call the method once, at the beginning of the program, to print a welcome message and once, at the end of the program to print the Thank you message. 一种方法必须在屏幕上显示一条消息,我必须在程序开始时调用一次该方法以打印欢迎消息,然后在程序结束时一次调用该方法以打印“谢谢”消息。 With that, I am lost on how to make it so a single method can determine when it is the end of the program. 这样,我就不知道如何制作它,因此一个方法可以确定何时结束程序。 (When the user enters a -1 when prompted to enter their name, the program will end.) (当提示输入用户名时用户输入-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;
}


}

You could make your printMessage method (renamed from welcomeMessage) take a boolean parameter that tells the method if it should display the welcome or thank you message. 您可以使printMessage方法(从welcomeMessage重命名)采用一个布尔参数,该参数告诉该方法是否应显示欢迎消息或感谢消息。

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

Then you call the method with true at the beginning of your program, and with false at the end. 然后,您在程序开始时调用true ,然后在结束时调用false

Alternatively you could have a class variable: 或者,您可以使用一个类变量:

private boolean hasPrintedWelcome = false;

And the printMessage method would be: 而printMessage方法将是:

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

The first time the printMessage method is called, it will display the welcome message. 第一次调用printMessage方法时,它将显示欢迎消息。 Then the second time the method is called, and any subsequent times, the method will display the thank you message. 然后第二次调用该方法,以后再调用一次,该方法将显示“谢谢”消息。

There is one problem with your program: you ask for the employee's name before entering in the while loop, so the name will be set only one time and never change, and so you will have an infinite loop. 程序存在一个问题:您需要进入while循环之前询问员工的姓名,因此姓名将仅设置一次且永不更改,因此将出现无限循环。

What you have to do is to put another getName() call at the end of the loop, to allow the user to set a new name, and exit the loop: 您要做的是在循环的末尾放置另一个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();

And once you have fixed that, you can just call the method showing the exit message after the while loop. 修复此问题后,您可以在while循环之后调用显示退出消息的方法。

EDIT: It seems that I misunderstood the question, and that the infinite loop was just a bug and not the actual question, see the other answer. 编辑:似乎我误解了这个问题,并且无限循环只是一个错误,而不是实际的问题,请参阅另一个答案。

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

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