我有这个程序来反转单词的顺序,但输入搞砸了。这是我的输出日志。
1
this is a trial
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Store_Credit.main(Store_Credit.java:13)
Process finished with exit code 1
而且,这是我的代码:
import java.util.Scanner;
public class Reverse_Words {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N;
N = scanner.nextInt();
for(int i = 0; i < N; i++) {
int h = i+1;
String[] s = scanner.nextLine().split(" ");
System.out.print("Case #"+h+": ");
for(int j = s.length-1; j >=0; j++) {
System.out.print(s[j]+" ");
}
System.out.println();
}
}
}
答案 0 :(得分:-1)
尝试执行此操作并检查是否仍然出现错误
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
int n;
System.out.println("enter number");
n = scanner.nextInt();
for(int i = 0; i < n; i++) {
int h = i+1;
System.out.println("enter string");
String[] s = scanner1.nextLine().split(" ");
System.out.print(s.length);
System.out.print("Case #"+h+": ");
for(int j = s.length-1; j >=0; j--) {
System.out.print(s[j]+" ");
}
System.out.println();
}
}
在最后一个for循环中递减j值,即。,j--(不是j ++)