英文:
I am a beginner programmer and I don't know how parameters work in Java. So how do they work?
问题
我正在使用Codecademy,但每当涉及参数时,我总是卡住。我观看了他们的视频,但我不明白。请帮助。
英文:
I am using code academy and I always get stuck when it comes to parameters. I watched their video but I didn't get it.Please help.
答案1
得分: 2
首先,它是 参数,而不是周长。
我们来举个例子。我们正在定义一个函数,该函数接受一个名字作为输入,并且作为输出,它返回一条问候消息。
class GreetUser {
/*
A Simple function which accepts a username as input and returns me back with a Hello message
*/
public static String greet(String username) { // function declaration
return "Hello, " + username;
}
public static void main(String[] args) {
String greetingMessage = greet("Daniel"); // 调用函数
System.out.println(greetingMessage);
}
}
- 行:
public static String greet(String username)
被称为函数定义。你在括号中传递的内容被称为 - 参数。它基本上是你定义的一些变量/数据占位符。用简单的英语来说,这个函数做某些事情并且返回某些东西,我在括号中定义的就是这个函数的输入。这里的greet
方法表示,我期望一个类型为 String 的输入。这就是参数。 - 程序中还有另一个叫做
main
的方法。一旦你理解了第一点,你就能自己回答主函数需要什么类型的参数。 - 有时候函数不需要输入,因此你不会向函数传递任何参数列表。例如:
public static void iDoNothing(){
}
- 在
main()
内部,我们调用了greet
函数并向它传递了一些值,因为它期望一个参数。你传递的内容在技术上被称为参数。
所以当你声明一个函数时,括号中的内容是它所期望的输入,被称为参数。
当你调用函数时,你向函数传递值,被称为参数。
请阅读更多内容:
另外,你应该阅读论坛关于 如何提问 的指南。
英文:
To begin with, it is parameter and not perimeter.
Let us take an example. We are defining a function which accepts a name as input and as an output, it returns a greeting message.
class GreetUser {
/*
A Simple function which accepts a username as input and returns me back with a Hello message
*/
public static String greet(String username) { // function declaration
return "Hello, " + username;
}
public static void main(String[] args) {
String greetingMessage = greet("Daniel"); // involing the function
System.out.println(greetingMessage);
}
}
- The line:
public static String greet(String username)
is called function definition. What you pass in the brackets is/are called - parameters. It is basically some variables/data placeholders you define. In plain English it means - this a function does something and returns something, and what I defined in the brackets are the inputs of this function. The greet method here says, I am expecting an input of the type String. This is the parameter - There is another method in the program called main Once you understand point one, you can answer for yourself what is the type of parameter main function takes in .
- There can be times when functions do no need an input, so you do not pass any parameter list to the function. Example:
public static void iDoNothing(){
}
- You can see inside main() we are invoking or calling the greet function and passing some value to it since it expects one parameter. What you pass, it is technically called argument
So when you declare a function, in the brackets you give what input it expects and is called parameter
When you call the function, you pass values to the function and it is called argument
Please read more on this:
Also, you should read the Guidelines of the forum about How to Ask a Good Question
答案2
得分: 0
关于你所说的参数,参数基本上是传递给方法并在方法中进行操作的值或对象。有时方法会返回这些值。当相同的代码需要针对不同的值重复使用时,通常会使用方法 - 比如我们需要计算两个值中的较大值 -
public static int greaterThan(int a, int b) {
if(a > b)
return a;
else
return b;
}
a 和 b 只是参数的名称,就像我们声明变量并为变量赋值一样。你可以多次重用这个方法 -
greaterThan(3, 5);
greaterThan(8, 5);
结果会不同 -
5
8
希望这个解释能帮助你理解参数。
谢谢
英文:
Are you talking about parameters? Parameters are basically values or objects passed to the methods and manipulate in the methods. Sometimes methods return the values. Methods are always used when the same code is to be used again and again for different values - like if we have to calculate greater of two values -
public static int greaterThan(int a, int b) {
if(a > b)
return a;
else
return b;
}
a and b are only the name of the parameters, like we declare the variables and assign the values to the variables. And you can reuse the method more than one time -
greaterThan(3,5);
greaterThan(8,5);
And the results will be different -
5
8
I hope this explanation will help you to understand about the parameters.
Thanks
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论