我正在通过参与UC Berkeley的2016年春季CS61B的一些项目来学习Java。 我正在这里概述的Nbody项目的“ readBodies”功能中: https://sp19.datastructur.es/materials/proj/proj0/proj0#readbodies
我跑步
javac Body.java NBody.java TestReadBodies.java
然后
java TestReadBodies
使用TestReadBodies测试我编写的NBody程序。然后,我得到了:
Checking readBodies...
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)
at In.readDouble(In.java:270)
at NBody.readBodies(NBody.java:17)
at TestReadBodies.checkReadBodys(TestReadBodies.java:21)
at TestReadBodies.main(TestReadBodies.java:93)
我认为问题可能是我的阅读类型错误,所以我改变了
的类型double xPP = in.readDouble();
double yPP = in.readDouble();
double xVV = in.readDouble();
double yVV = in.readDouble();
double masss = in.readDouble();
转换为int,long和short,但似乎不起作用。我必须使用“ In”类来解决此问题。这是完整的文档:
https://introcs.cs.princeton.edu/java/stdlib/javadoc/In.html
我也不允许使用
System.exit(0)
这是Nbody.java
public class NBody {
public static double readRadius(String fileName) {
In in = new In(fileName);
in.readInt();
double radius = in.readDouble();
return radius;
}
public static Body[] readBodies(String fileName) {
In in = new In(fileName);
int size = in.readInt();
Body[] list = new Body[size];
int count = 0;
double test = in.readDouble();
while(!in.isEmpty()) {
double xPP = in.readDouble();
double yPP = in.readDouble();
double xVV = in.readDouble();
double yVV = in.readDouble();
double masss = in.readDouble();
String imgFilee = in.readString();
Body planett = new Body(xPP, yPP, xVV, yVV, masss, imgFilee);
list[count] = planett;
count += 1;
}
return list;
}
这是TestReadBodies.java
/**
* Tests Nbody.readBodies. Reads in ./data/planets.txt and checks output of
* readBodies().
*/
public class TestReadBodies {
private static boolean doubleEquals(double actual, double expected, double eps) {
if (Double.isNaN(actual) || Double.isInfinite(actual)) {
return false;
} else {
return Math.abs(expected - actual) <= eps * Math.max(expected, actual);
}
}
/** Checks to make sure that readBodies() works perfectly. */
private static String checkReadBodys() {
System.out.println("Checking readBodies...");
String planetsTxtPath = "./data/planets.txt";
/* If the following line fails to compile, you probably need to make
* a certain method static! */
Body[] actualOutput = NBody.readBodies(planetsTxtPath);
/* Check the simple things: */
if (actualOutput == null) {
return "FAIL: readBodies(); null output";
}
if (actualOutput.length != 5) {
return "FAIL: readBodies().length: Expected 5 and you gave " + actualOutput.length;
}
/* Check to make sure every planet exists, plus random spot checks */
boolean foundEarth = false;
boolean foundMars = false;
boolean foundMercury = false;
boolean foundSun = false;
boolean foundVenus = false;
boolean randomChecksOkay = true;
for (Body p : actualOutput) {
if ("earth.gif".equals(p.imgFileName)) {
foundEarth = true;
if (!doubleEquals(p.xxPos, 1.4960e+11, 0.01)) {
System.out.println("Advice: Your Earth doesn't have the right xxPos!");
randomChecksOkay = false;
}
} else if ("mars.gif".equals(p.imgFileName)) {
foundMars = true;
} else if ("mercury.gif".equals(p.imgFileName)) {
foundMercury = true;
if (!doubleEquals(p.yyPos, 0, 0.01)) {
System.out.println("Advice: Your Mercury doesn't have the right yyPos!");
randomChecksOkay = false;
}
} else if ("sun.gif".equals(p.imgFileName)) {
foundSun = true;
} else if ("venus.gif".equals(p.imgFileName)) {
foundVenus = true;
if (!doubleEquals(p.mass, 4.8690e+24, 0.01)) {
System.out.println("Advice: Your Venus doesn't have the right mass!");
randomChecksOkay = false;
}
}
}
/* Build up a nice list of missing planets */
String missingBodys = "";
if (!foundEarth) {
missingBodys += "Earth, ";
}
if (!foundMars) {
missingBodys += "Mars, ";
}
if (!foundMercury) {
missingBodys += "Mercury, ";
}
if (!foundSun) {
missingBodys += "Sun, ";
}
if (!foundVenus) {
missingBodys += "Venus, ";
}
if (missingBodys.length() > 0) {
String answer = "FAIL: readBodies(); Missing these planets: ";
answer += missingBodys.substring(0, missingBodys.length() - 2);
return answer;
}
if (!randomChecksOkay) {
return "FAIL: readBodies(); Not all planets have correct info!";
}
return "PASS: readBodies(); Congrats! This was the hardest test!";
}
public static void main(String[] args) {
String testResult = checkReadBodys();
System.out.println(testResult);
}
}
请让我知道为什么会出现此错误,以及如何在Nbody.java中修复它。
非常感谢您!