我试图创建一个以正整数作为参数的函数,然后将该整数作为多个输入。每个输入都将在新行上。然后将这些输入(字符串)附加到列表并返回
这就是函数应该是这样的:
4
a
b
c
d
["a", "b", "c", "d"]
2
q
r
["q", "r"]
答案 0 :(得分:1)
最好使用4种方法来解决这个问题:
//
//你没有指定,但我假设java并且你想使用数组
//我编写了整个程序,并愿意修复/帮助您发布的任何代码
//还记得导入扫描仪。
//
1)主要很简单,只需调用方法:
public static void main(String[] args) {
int length = test();
String[] array = arrayCreator(length);
printArray(array);
}
2)现在创建数组:
- public static String[] arrayCreator(int length){
- create a new scanner
- make a new array with the int "length" defining its length.
- Prompt user to enter strings
- simple for loop with i being less than length
- inside for loop:
- new string getting input from scanner
- array[i] = temp;
- return the array
3)打印数组:
- public static void printArray(String[] array){
- int length = array.length;
- String temp = "";
- System.out.print("[");
- for loop with i < length
- nested in for loop:
- if statement with i equaling one less than the length:
- temp = array[i];
- System.out.print("\""+temp+"\"]");
- else statement:
- temp = array[i];
- System.out.print("\""+temp+"\", ");
4)测试方法:
- public static int test(){
- Create a new Scanner (remember to import scanner)
- prompt for array size
- new int that takes the input
- return the int