嗯,每当使用扫描仪输入带空格的Java代码进行编码时,我总是会遇到问题。每当我在codechef,codeforce或大多数在线编码站点中进行编码时,都会发生这种情况。您能否建议最佳的处理方式:-
测试案例:
1
6
112 243 512 343 90 478
500 789 234 400 452 150
代码:
import java.io.*;
import java.util.*;
public class CandidateCode {
public static void main(String args[] ) throws Exception {
Scanner sc= new Scanner(System.in);
int T=sc.nextInt();
int N=sc.nextInt();
int arr[]=new int[N];
int sum=0;
String strength=sc.nextLine();
String energy=sc.nextLine();
Scanner st= new Scanner(strength);
Scanner en= new Scanner(energy);
for(int i=0;i<N;i++)
{
if(st.nextInt()<en.nextInt())
arr[i]=1;
else
arr[i]=0;
}
for(int j=0;j<N;j++)
sum+=arr[j];
if(sum==N)
System.out.println("\nWIN");
else System.out.println("\nLOSE");
sc.close();
}
}
Exception:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
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 CandidateCode.main(CandidateCode.java:21)
Compilation Log
--Code Compiled successfully--