所以我正在为我的计算机科学课编写一个程序,我没有错误,但它不会运行。我也必须使用in文件。 我想做的项目就在这里 http://www.doralacademyprep.org/ourpages/auto/2016/10/28/50508547/C1%20PRG%20JR.pdf
错误信息在这里
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 GeeImATree.main(GeeImATree.java:58)
以下是代码:
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class GeeImATree {
public static boolean isEquilateral(double sideA, double sideB, double sideC) {
if(sideA == sideB && sideB == sideC) {
return true;
}
return false;
}
public static boolean isIsosceles(double sideA, double sideB, double sideC) {
if(!isEquilateral(sideA,sideB,sideC)){
return (sideA == sideB || sideB == sideC || sideA == sideC);
}
return false;
}
public static boolean isRight(double sideA, double sideB, double sideC) {
if(!isEquilateral(sideA,sideB,sideC) && !isIsosceles(sideA,sideB,sideC)){
return ( (sideA*sideA) == (sideB*sideB)+(sideC*sideC) || (sideB*sideB) == (sideA*sideA)+(sideC*sideC) || (sideC*sideC) == (sideA*sideA)+(sideB*sideB));
}
return false;
}
public static boolean isObtuse(double sideA, double sideB, double sideC) {
if (!isEquilateral(sideA,sideB,sideC) && !isIsosceles(sideA,sideB,sideC) && !isRight(sideA,sideB,sideC)) {
return ( (sideA*sideA) > (sideB*sideB)+(sideC*sideC) || (sideB*sideB) > (sideA*sideA) + (sideC*sideC) || (sideC*sideC) > (sideA*sideA)+(sideB*sideB) );
}
return false;
}
public static boolean isAcute(double sideA, double b, double c) {
if(!isEquilateral(sideA,b,c) && !isIsosceles(sideA,b,c) && !isRight(sideA,b,c) && !isObtuse(sideA,b,c)){
return( (sideA*sideA) < (b*b)+(c*c) || (b*b) < (sideA*sideA) + (c*c) || (c*c) < (sideA*sideA)+(b*b) );
}
return false;
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File infile = new File("GEOMETRY.IN");
Scanner scan = new Scanner(infile);
double x1, y1, x2, y2, x3, y3;
while(scan.hasNext())
{
x1 = scan.nextInt();
y1 = scan.nextInt();
x2 = scan.nextInt();
y2 = scan.nextInt();
x3 = scan.nextInt();
y3 = scan.nextInt();
double sideA = Math.sqrt( Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2) );
double sideB = Math.sqrt( Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2) );
double sideC = Math.sqrt( Math.pow(x3 - x2, 2) + Math.pow(y3 - y2, 2) );
System.out.println("a= "+sideA+", b= "+sideB+", c= "+sideC);
if (isEquilateral(sideA,sideB,sideC))
System.out.println("Equilateral");
if (isIsosceles(sideA,sideB,sideC))
System.out.println("Isosceles");
if (isRight(sideA,sideB,sideC))
System.out.println("Right");
if (isObtuse(sideA,sideB,sideC))
System.out.println("Obtuse");
if (isAcute(sideA,sideB,sideC))
System.out.println("Acute little triangle");
System.out.println("/n-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_");
}
scan.close();
}
}
在Geometry.IN文件中有以下
0 0 3.5 1.2 9.85 2
0 0 5 0 5 9
0 0 6 8 8 0
0 0 3 7 1 7
0 0 8 4 8 -4
答案 0 :(得分:0)
你应该使用nextDouble():
x1 = scan.nextDouble();
y1 = scan.nextDouble();
x2 = scan.nextDouble();
y2 = scan.nextDouble();
x3 = scan.nextDouble();
y3 = scan.nextDouble();
并尝试使用此文件:
0 0 3,5 1,2 9,85 2
0 0 5 0 5 9
0 0 6 8 8 0
0 0 3 7 1 7
0 0 8 4 8 -4
此外,您必须舍入乘法结果,例如:
public static boolean isRight(double sideA, double sideB, double sideC) {
if(!isEquilateral(sideA,sideB,sideC) && !isIsosceles(sideA,sideB,sideC)){
return ( Math.round(sideA*sideA) == Math.round(sideB*sideB)+Math.round(sideC*sideC) || Math.round(sideB*sideB) == Math.round(sideA*sideA)+Math.round(sideC*sideC) || Math.round(sideC*sideC) == Math.round(sideA*sideA)+Math.round(sideB*sideB));
}
return false;