gpt4 book ai didi

java - 使用文件编号计算 GCM - NoSuchElementException

转载 作者:行者123 更新时间:2023-12-01 21:08:30 26 4
gpt4 key购买 nike

我正在尝试将数字放入文件中并相应地计算 GCM。但是我不断收到 NoSuchElementException。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Prog280a {

public static void main(String[] args) {
Scanner in = null;
int greater = 1;
int lesser = 1;
int a = 1;
int b = 1;
int originalA = 0;
int originalB = 0;

try {
in = new Scanner(new File(("C:\\Users\\####\\Desktop\\####\\Eclipse\\#######\\#######2\\src\\Prog280a.dat")));
} catch (FileNotFoundException e) {
System.out.println("File is not found try again.");
System.exit(0);
}

while (in.hasNextInt()) {
while (a != 0) {
greater = in.nextInt();
lesser = in.nextInt();
if (greater > lesser) {a = greater; b = lesser;} else {b = greater; a = lesser;}
originalA = a;
originalB = b;
a = a - b;
}
System.out.println("The GCD of " + originalA + " & " + originalB + " is " + b);
a = 1;
b = 1;
}

}

}

好的,就在我说的那一行。 '更大= in.nextInt();'系统说没有这样的元素异常(exception)。它为什么要这么做?请帮忙。

下面是它的内容:

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Prog280a.main(Prog280a.java:25)

顺便说一句,这些是文件中的数字:25 1081 41255 13884762484 420182637620 42036 2475 1251 17

最佳答案

我的假设是否正确,您不一定会在调用第一个 greater = in.nextInt() 时遇到错误?

因为我认为你的问题出在你的循环上。在外循环中,您正确地询问 in.hasNext() - 但是,在你的内部循环中,你似乎在循环 while a != 0 - 所以,您可以继续调用greater = in.nextInt(); lesser = in.nextInt();而不检查您的枚举是否有更多元素。

关于java - 使用文件编号计算 GCM - NoSuchElementException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41770695/

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