Java 为什么在方法中输入的最后一个字符串首先获得priint?
输出:Java 为什么在方法中输入的最后一个字符串首先获得priint?,java,Java,输出: Hey ^goooo^^glee^ ora^^nge^^^^ 我的输出: Hey google -1(since all char gets erased) final static Pattern=Pattern.compile(“\\^”,Pattern.MULTILINE); 公共静态void main(字符串[]args){ 扫描仪sc=新的扫描仪(System.in); int N=sc.nextInt();//因为\N附加了前面的nextInt sc.nextLine()
Hey ^goooo^^glee^
ora^^nge^^^^
我的输出:
Hey google
-1(since all char gets erased)
final static Pattern=Pattern.compile(“\\^”,Pattern.MULTILINE);
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
int N=sc.nextInt();//因为\N附加了前面的nextInt
sc.nextLine();
字符串[]输出=新字符串[N];
for(int i=0;i
查看这是否有助于您您实际上是在为空的
字符串调用undoOper()
,然后将其称为“Hey^goooo^^glee^”。添加sc.nextLine()
在第一个循环之前。您的输入引发异常,因为nextInt()
不喜欢标记Hey
@Andreas我假设OP也输入了2作为第一个输入,并且忘记了提到它(考虑到报告的输出)。@Eran我同意您的假设,但这并不意味着这个问题是个好问题。在编程中,你写的东西的准确性很重要,因为计算机会做你说的,而不是你的意思。人们在这里问编程问题时应该使用同样的表达精度。我们都可以猜,但我们不需要猜,所以我向OP指出这个问题需要改进。如果不让OP知道,就不能期望改进。
Hey google
-1(since all char gets erased)
-1
Hey google
final static Pattern pattern = Pattern.compile("\\^", Pattern.MULTILINE);
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt(); //because \n is appended with previous nextInt
sc.nextLine();
String[] outPut = new String[N];
for (int i = 0; i < outPut.length; i++) {
String input = sc.nextLine();
Matcher matcher = pattern.matcher(input);
input = matcher.replaceAll("");
outPut[i] = input;
}
sc.close();
System.out.println(Arrays.asList(outPut));
}