如何将索引、名称和编号作为参数?
How to take index, name and number as parameters?
嗨,我在处理中接到了一个任务,问题是这个 "You are asked to store a small telephone contact list that can hold ten names and ten matching telephone numbers. show how you would write a function that takes a name, a telephone number and index as parameters and sets the array(s) at the index position to the values given. Include in your answer code that checks the index given is a valid position in your array(s)."
我想出了下面的代码
String[] names = new String[10];
int[] numbers = new int[10];
String[] contact = new String[10];
void setup() {
names[0] = "p1";
names[1] = "p2";
names[2] = "p3";
names[3] = "p4";
names[4] = "p5";
names[5] = "p6";
names[6] = "p7";
names[7] = "p8";
names[8] = "p9";
names[9] = "p10";
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
numbers[3] = 4;
numbers[4] = 5;
numbers[5] = 6;
numbers[6] = 7;
numbers[7] = 8;
numbers[8] = 9;
numbers[9] = 10;
for (int i=0; i<10; i++) {
contact[i] = "Name:"+names[i] +" "+ "Number:" + numbers[i]+" ";
println(contact[i]);
}
}
但是我的老师对我的代码的评论是这样的"So what I expected here was a simple function that took an index, a number and a name as parameters and then filled in the arrays at the index value with the values."我对这个问题很困惑,void setup 不是一个函数吗?我真的不知道将索引、数字和名称作为参数是什么意思。所以如果有人能指出我谢谢!
编辑:改进代码
void setup() {
for (int i=0; i<10; i++){
contactList(i, "aName", 123456789);//default value to all 10 elememnts
}
}
void contactList(int index, String name, int number) {
println (index, "Name:" + name, "Number:" + number);
}
你知道怎么写函数吗?提示:是的,您编写了 setup() 函数!你能写另一个带参数的函数吗?从一个简单的函数开始,它只接受一个参数并将其打印出来。从那里开始工作。
这里有一个小例子,它将一个字符串作为参数并将其打印出来,它是从 setup() 函数调用的:
void setup(){
printMe("hello!");
}
void printMe(String text){
println(text);
}
推荐阅读:
Passing Information to a Method or a Constructor - Java Tutorials
嗨,我在处理中接到了一个任务,问题是这个 "You are asked to store a small telephone contact list that can hold ten names and ten matching telephone numbers. show how you would write a function that takes a name, a telephone number and index as parameters and sets the array(s) at the index position to the values given. Include in your answer code that checks the index given is a valid position in your array(s)."
我想出了下面的代码
String[] names = new String[10];
int[] numbers = new int[10];
String[] contact = new String[10];
void setup() {
names[0] = "p1";
names[1] = "p2";
names[2] = "p3";
names[3] = "p4";
names[4] = "p5";
names[5] = "p6";
names[6] = "p7";
names[7] = "p8";
names[8] = "p9";
names[9] = "p10";
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
numbers[3] = 4;
numbers[4] = 5;
numbers[5] = 6;
numbers[6] = 7;
numbers[7] = 8;
numbers[8] = 9;
numbers[9] = 10;
for (int i=0; i<10; i++) {
contact[i] = "Name:"+names[i] +" "+ "Number:" + numbers[i]+" ";
println(contact[i]);
}
}
但是我的老师对我的代码的评论是这样的"So what I expected here was a simple function that took an index, a number and a name as parameters and then filled in the arrays at the index value with the values."我对这个问题很困惑,void setup 不是一个函数吗?我真的不知道将索引、数字和名称作为参数是什么意思。所以如果有人能指出我谢谢!
编辑:改进代码
void setup() {
for (int i=0; i<10; i++){
contactList(i, "aName", 123456789);//default value to all 10 elememnts
}
}
void contactList(int index, String name, int number) {
println (index, "Name:" + name, "Number:" + number);
}
你知道怎么写函数吗?提示:是的,您编写了 setup() 函数!你能写另一个带参数的函数吗?从一个简单的函数开始,它只接受一个参数并将其打印出来。从那里开始工作。
这里有一个小例子,它将一个字符串作为参数并将其打印出来,它是从 setup() 函数调用的:
void setup(){
printMe("hello!");
}
void printMe(String text){
println(text);
}
推荐阅读:
Passing Information to a Method or a Constructor - Java Tutorials