[英]How to take array elements as input
My program is to accept words (given as number of test cases) and print them out in reversed order. 我的程序是接受单词(以测试用例的数量给出)并以相反的顺序打印出来。 The problem is that whatever input of array size I may give, it only accepts just one word (and rest as blank). 问题是,无论我提供多少数组大小的输入,它都只接受一个单词(其余为空白)。 Can anyone help me figure out why? 谁能帮我找出原因? Here's the code: 这是代码:
import java.util.*;
public class terrible {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int test = input.nextInt();
while(test>0){
String str = input.nextLine();char c[] = str.toCharArray();
for(int i=0;i<str.length();i++){
System.out.print(c[str.length()-i-1]);
}
System.out.println();
test--;
}
}
}
Certain versions of Java don't let you take an int
and then a string
as input from the same Scanner
. Java的某些版本不允许您将int
和string
作为来自同一Scanner
输入。 You can create another Scanner
, like 您可以创建另一个Scanner
,例如
Scanner input2 = new Scanner(System.in);
and then do 然后做
String str = input2.nextLine();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.