我给出了一个总数和我从输入中得到的那么多字符串。如何同时打印所有输出
我的代码是:
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int tot = sc.nextInt();
for(int i=0; i<r; i++){
int L;
String str = sc.next();
if(L == str.length()) {
print(str);
}
}
}
private static void print(String str){
System.out.println("String is "+ str);
}
在这段代码我的字符串印刷我给输入之后。
OutPut : 3
"ABC"
String is ABC
"ABCD"
String is ABCD
"ABCDE"
String is ABCDE
但是我想要的是先取得所有的输入然后打印;
例:
OutPut : 3
"ABC"
"ABCD"
"ABCDE"
String is ABC
String is ABCD
String is ABCDE
可有人请解释一下,帮我如何修改我的字符串。
注:我的想法是把所有的东西放入堆栈,但不知道打印谁。我知道我的结果具有因的到来for循环,但不知道如何从
哪里是打印方法? –
使用'String [] str'数组并添加像这样的'str [i] = sc.next();'。在你的循环调用'print(str);'后把方法的参数改成字符串数组。然后迭代数组并打印每个值 – XtremeBaumer
@ΦXocę웃Пepeúpaツ我更新我的问题 –