Error java.util.NoSuchElementException in java



我正在实现一个计算圆和矩形的面积和周长的程序。半径(圆),宽度,高度(矩形)从用户获取。问题是:在我输入半径参数后,控制台弹出消息:

线程"main"异常java.util.NoSuchElementException在…

main .java位于main包,Circle.java和Rectangle.java位于shapes包。

Main.java:

package main;
import shapes.Circle;
import shapes.Rectangle;
//import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Circle cir = new Circle();
Rectangle rec = new Rectangle();
cir.printArea();
cir.printPerimeter();
rec.printArea();
rec.printPerimeter();
}
}

Circle.java:

package shapes;
import java.util.Scanner;
public class Circle {
final double PI = 3.14;
double radius;
double area, perimeter;
public Circle(double radius) {
this.radius = radius;
area = PI*radius*radius;
perimeter = 2*PI*radius;
}
public Circle() {
System.out.print("radius = ");
Scanner scan = new Scanner(System.in);
radius = scan.nextDouble();
scan.close();
area = PI*radius*radius;
perimeter = 2*PI*radius;
}
public void printArea() {
System.out.println("Area of circle = " + area);
}
public void printPerimeter() {
System.out.println("Perimeter of circle = " + perimeter);
}
}

Rectangle.java:

package shapes;
import java.util.Scanner;
public class Rectangle {
public double length, width;
double area, perimeter;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
area = length * width;
perimeter = (length + width) * 2;
}
public Rectangle() {
Scanner scan = new Scanner(System.in);
System.out.print("width = ");
width = scan.nextDouble();
System.out.print("length = ");
length = scan.nextDouble();
scan.close();
area = length * width;
perimeter = (length + width) * 2;
}
public void printArea() {
System.out.println("Area of rectangle = " + area);
}
public void printPerimeter() {
System.out.println("Perimeter of rectangle = " + perimeter);
}
}

为什么会发生这种情况以及如何修复它?

  1. 你的扫描仪应该在主类和静态,你只使用一个,不需要更多
  2. Circle和Rectangle有一些应该外包的基本方法,所以使用一个接口,或者在这种情况下最好是一个抽象类,如Geoform作为超类。
  3. 不要关闭扫描器,这根本没有必要。
  4. 主:

public class Main {
private static final Scanner scan = new Scanner(System.in);
private static Circle createCircle(){
System.out.print("radius = ");
double radius = scan.nextDouble();
return new Circle(radius);
}
private static Rectangle createRectangle() {
System.out.print("width = ");
double width = scan.nextDouble();
System.out.print("length = ");
double length = scan.nextDouble();

return new Rectangle(width, length);
}
public static void main(String[] args) {
Circle cir = createCircle();
Rectangle rec = createRectangle();
cir.printArea();
cir.printPerimeter();
rec.printArea();
rec.printPerimeter();
}
}

Geoform:

public abstract class Geoform {
final double area;
final double perimeter;
public Geoform(double area, double perimeter) {
this.area = area;
this.perimeter = perimeter;
}
public void printArea() {
System.out.println("Area = " + area);
}
public void printPerimeter() {
System.out.println("Perimeter = " + perimeter);
}
}

循环:

public class Circle extends Geoform{
public static final double PI = 3.14;
private final double radius;
public Circle(double radius) {
super(PI*radius*radius, 2*PI*radius);
this.radius = radius;
}
public double getRadius() {
return radius;
}
}
矩形:

public class Rectangle extends Geoform {
private final double length, width;
public Rectangle(double width, double length) {
super(length * width, (length + width) * 2);
this.width = width;
this.length = length;
}
public double getLength() {
return length;
}
public double getWidth() {
return width;
}
}

你有几个问题…

首先,你需要学习如何编写更清晰的代码,比如:圆,而不是"cir"like Circle =new Circle();其次,我们通常不在构造函数中使用Scanner尽量在方法内使用。或者为输入读取创建一个方法。第三,不要关闭扫描仪。(很可能这就是错误的原因)。试着注释你的代码..

相关内容

  • 没有找到相关文章

最新更新