课程: Learning Java 17
Parameters in Java
- [Instructor] The announceTeatime function had no inputs. It was just a series of steps that the program followed every time the function was called. The output was always the same. There was nothing dynamic about it. What if we wanted the output to be different depending on what was inputted into the function? For example, let's say we wanted to calculate the total cost of a meal, including tip and tax. To calculate this, we would first figure out the tip by multiplying the tip rate with the listed price of the meal, then we'd figure out the tax by multiplying the tax rate with the listed price. At the end, we would add the tip, tax and listed price and then output the result. These are finite steps that we can label as calculateTotalMealPrice. Of course, almost every meal you order will have a different listed price, and sometimes you might want to tip at a different rate depending on the service you get. Sales tax rates are also different depending on what state or country you're in. How can we account for this in our function? What value could we give listed meal price, tip rate and tax rate in our function if it changes every time? In this lesson, we're going to create a function with inputs. Adding inputs is going to allow us to insert a specific listed meal price, tip rate and tax rate into the function every time we use it. The listed price, tip rate and tax rate inputs will be defined in the function's definition, but the value of these inputs will be assigned when we call the function. Let's try implementing this in Java. Our function will start off with public static void, and it'll have the name TotalMealPrice. Using our formula from before, we'll calculate the tip by multiplying the tip rate with the listed price, then we'll calculate the tax by multiplying the tax rate with the listed price. Finally, we'll calculate the result. We'll add together listed price, tip and tax. We'll also output it to the console. Your total meal price is the result. Now, you might notice that the listed price, tip rate and tax rate are red, and that's because we have not defined these as inputs to our function. To define an input, we add code between the parentheses. The first input will be listed price. It'll have the data type "double" since it could be a decimal number. So we just defined our first input, and now listed price is no longer red. Let's do this for tip rate and tax rate. These will also be doubles. Thinking back to our discussion of scope, these inputs are only accessible within the function's implementation. We'll give these inputs a value later on, but since they're created here as a part of the function's definition, they can only be used within the function. Now that our inputs are defined, let's use the function. So if we try calling this function like announceTeatime, it won't work. Unlike announceTeatime, we define inputs to this function, and we have to give a value for each of those inputs. The first input is listed price. We can give this the value 15 by adding 15 within the parentheses, but we still have an error. That's because we need to add a value for each input, not just the first. We can add a .2 tip rate and a .08 tax rate. Each input now has a value for this function call. So the error goes away. It's important to remember that order does matter here. So the first input defined in the function will get the first value in the parentheses, same for the second and for the third. Let's run this code and see what happens, and we get an output. Your total meal cost is 19.2. Let's try changing the values of these inputs and see what happens. Instead of 15, we'll say the listed price is 25. We'll only want to tip 18% or .18, and we'll keep the same tax rate. Let's run it again. The total meal price is now 31.5. Changing the inputs to our calculateTotalMealPrice function affects the output. This is the first time we've used a function with different input values. The function's definition had a set of defined steps, calculating the tip tax and result and then finally displaying them to the console. The function also had a defined list of parameters, listed price, tip rate and tax rate. We called the function with different arguments or different values each time. This resulted in different outputs to the console.
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。