gpt4 book ai didi

java.util.NoSuchElementException(运行时错误)

转载 作者:行者123 更新时间:2023-11-30 03:23:42 25 4
gpt4 key购买 nike

当我将以下代码提交到 codeforces.com ( http://codeforces.com/problemset/problem/3/A ) 时:

import java.util.Scanner;
public class A3 {

public static void main(String[] args) {
Scanner s=new Scanner(System.in).useDelimiter("");
Scanner t=new Scanner(System.in).useDelimiter("");
String a=s.next();
int y=s.nextInt();
int x = (int)a.charAt(0);
String d=t.next();
int fy=t.nextInt();
int fx = (int)d.charAt(0);
int n=0;
if (Math.abs(fx-x)>Math.abs(fy-y))
{
n=Math.abs(fx-x);
}
else
{
n= Math.abs(fy-y);
}
System.out.printf("%d\n",n);
for (int counter=1;counter<=n;++counter)
{
if (fx<x)
{
System.out.printf("L");
x--;
}
else if (fx>x)
{
System.out.printf("R");
x++;
}
if (fy<y)
{
System.out.printf("D");
y--;
}
else if (fy>y)
{
System.out.printf("U");
}
System.out.println();
}
s.close();
t.close();

}
}

,它说没有找到这样的元素,尽管它在 Eclipse 和 JCreator IDE 中运行得很好。那么有人可以让我知道我哪里做错了吗?提前致谢 :)完整的错误代码:

java.util.NoSuchElementException

at java.util.Scanner.throwFor(Scanner.java:862)

at java.util.Scanner.next(Scanner.java:1371)

at A3.main(A3.java:10)

最佳答案

你想读取2行输入,我相信你可以使用BufferedReaderStringTokenizer:

代替下面的代码:

Scanner s=new Scanner(System.in).useDelimiter("");
Scanner t=new Scanner(System.in).useDelimiter("");
String a=s.next();
int y=s.nextInt();
int x = (int)a.charAt(0);
String d=t.next();
int fy=t.nextInt();
int fx = (int)d.charAt(0);

您可以使用以下内容:

BufferedReader br  = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer str = new StringTokenizer(br.readLine());
String a = str.nextToken();
int y = Integer.parseInt(str.nextToken);
int x = (int)a.charAt(0);//same as earlier

str = new StringTokenizer(br.readLine());
String d = str.nextToken();
int fy = Integer.parseInt(str.nextToken);
int fx = (int)d.charAt(0);//same as earlier

这应该足以完成您想要的操作,而且与扫描仪相比它速度更快。您可以查看this文章,它展示了在 Java 中读取输入的不同方法,还说明了哪种方法在速度方面更好:

关于java.util.NoSuchElementException(运行时错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30751654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com