我用一个单独的类编写了一个程序但是在用户输入三个方面之后我一直得到同样的错误。
主要代码是:
package interactiveTriangleWithAClass;
public class InteractiveTriangleProgramClass
{ public static void main (String [] args) throws Exception
{ IAclass nums = new IAclass();
double perimeter;
double area;
explain();
nums.getNumbers();
perimeter = nums.calcPer();
area = nums.calcArea();
outputResults(nums, perimeter, area); }
public static void explain()
{ System.out.println("This program calculates the perimeter ");
System.out.print("and the area of 3 sides for a triangle. ");
System.out.println("The 3 numbers are entered by the user.\n"
+ "The output is the 3 numbers, the perimeter and the area.\n"
+ "A seperate class is used.\n"
+ "Program written by Derek Michel"); }
public static void outputResults(IAclass nums, double area, double perimeter) throws Exception
{ nums.getNumbers();
System.out.println();
System.out.println("The perimeter is " + perimeter);
System.out.print(" and the area is " + area); }
}
课程是:
package interactiveTriangleWithAClass;
import java.util.*;
public class IAclass {
private double num1, num2, num3;
public IAclass() {
num1 = num2 = num3 = 3;
}
public IAclass(double value1, double value2, double value3) {
num1 = value1;
num2 = value2;
num3 = value3;
}
public void getNumbers() throws Exception {
Scanner console = new Scanner(System. in );
System.out.println("Enter the first side.");
num1 = console.nextDouble();
System.out.println("Enter the second side.");
num2 = console.nextDouble();
System.out.println("Enter the third side.");
num3 = console.nextDouble();
console.close();
while (num1 <= 0) {
System.err.print("Error. Side one must be greater than 0.");
System.out.println("Enter side one again.");
num1 = console.nextDouble();
}
while (num2 <= 0) {
System.err.print("Error. Side two must be greater than 0.");
System.out.println("Enter side two again.");
num2 = console.nextDouble();
}
while (num3 <= 0) {
System.err.print("Error. Side three must be greater than 0.");
System.out.println("Enter side three again.");
num3 = console.nextDouble();
}
if (num1 + num2 < num3) {
System.out.println("Based on the sides inputed, your triangle ");
System.out.print("is impossible to create.");
System.exit(0);
}
if (num2 + num3 < num1) {
System.out.println("Based on the sides inputed, your triangle ");
System.out.print("is impossible to create.");
System.exit(0);
}
if (num1 + num3 < num2) {
System.out.println("Based on the sides inputed, your triangle ");
System.out.print("is impossible to create.");
System.exit(0);
}
}
public double calcPer() {
return (num1 + num2 + num3);
}
public double calcArea() {
double s;
s = (num1 + num2 + num3) / 2;
return (Math.sqrt(s * (s - num1) * (s - num2) * (s - num3)));
}
public void outputNumbers() {
System.out.print("The sides are " + num1 + ", " + num2);
System.out.print(" and" + num3);
}
}
控制台在运行后和3个侧面输入后读取此内容:
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.nextDouble(Unknown Source)
at interactiveTriangleWithAClass.IAclass.getNumbers(IAclass.java:48)
at interactiveTriangleWithAClass.InteractiveTriangleProgramClass.outputResults(InteractiveTriangleProgramClass.java:54)
at interactiveTriangleWithAClass.InteractiveTriangleProgramClass.main(InteractiveTriangleProgramClass.java:41)
答案 0 :(得分:1)
尝试删除以下行:
console.close();
关闭后,您无法从console
开始阅读。
您几乎肯定不应该明确关闭Scanner
:这样做会关闭System.in
。关闭一个你自己没有打开过的流并不是一个好习惯,因为其他一些代码可能依赖于正在打开的流,并且当它不是时会抛出异常。
调试这样一个问题 - 异常可能会远离原因 - 非常棘手。
答案 1 :(得分:0)
Scanner#nextDouble
抛出NoSuchElementException
如果输入用尽
避免它的方法,一般使用扫描仪(也适用于迭代器)的正确方法是首先检查是否有这样的输入可用。尝试:
if (scanner.hasNextDouble())
scanner.nextDouble();
如果通话返回false
,则根本没有double
可供阅读。