当我在循环的第一次迭代中运行代码时,它通常会读取该元素的值并将其存储在数组列表中。但在那之后,当我到达任何扫描仪时,任何扫描仪都不会给我任何输入变量的机会,并给出 NoSuchElementException
我期待的是这样的事情
输入形状数量:3
输入 1 代表圆形,2 代表矩形,3 代表正方形:1
输入半径:1
面积为3.14,周长为6.28
输入 1 表示圆形,2 表示矩形,3 表示正方形:2 输入长度和宽度:5 4
面积为20,周长为18
输入 1 表示圆形,2 表示矩形,3 表示正方形:3 输入边长:2
面积为4,周长为8
输入阈值:15
形状为矩形(5,4)
其他类矩形和正方形与圆形类似
class Circle
{
int radius;
public void readAttributes() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter Radius: ");
radius = scanner.nextInt();
scanner.close();
}
public double calculateArea() {
return Math.PI * radius * radius;
}
public double calculatePerimeter() {
return 2 * Math.PI * radius;
}
public void displayAttributes()
{
System.out.print("Circle("+this.radius+") ");
}
}
public class Q7 {
private static final DecimalFormat decfor = new DecimalFormat("0.00");
public static void main(String args[]) throws Exception {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of shapes: ");
int numshapes = scanner.nextInt();
int choice=0;
List<Object> shapes=new ArrayList<>();
for (int i = 0; i < numshapes; i++)
{
System.out.print("Enter 1 for Circle, 2 for Rectangle, 3 for Square: ");
//Second iteration there is NoSuchElementException
choice=scanner.nextInt();
switch (choice)
{
case 1:
Circle circle = new Circle();
circle.readAttributes();
System.out.println("The area is "+decfor.format(circle.calculateArea())+" and the perimeter is "+decfor.format(circle.calculatePerimeter()));
shapes.add(circle);
break;
case 2:
Rectangle rectangle = new Rectangle();
rectangle.readAttributes();
rectangle.displayAreaAndPerimeter();
shapes.add(rectangle);
break;
case 3:
Square square = new Square();
square.readAttributes();
square.displayAreaAndPerimeter();
shapes.add(square);
break;
default:
System.out.println("Invalid Choice! Try Again.");
break;
}
}
System.out.print("Enter the threshold value: ");
//If it reaches here also it gives NoSuchElementException
int threshold=scanner.nextInt();
System.out.print("The shapes are ");
for(Object shape:shapes){
if (shape instanceof Circle) {
Circle circle=(Circle) shape;
if((circle.calculateArea())>threshold)
circle.displayAttributes();
}
else if (shape instanceof Rectangle) {
Rectangle rectangle=(Rectangle) shape;
if((rectangle.calculateArea())>threshold)
rectangle.displayAttributes();
}
else if (shape instanceof Square) {
Square square=(Square) shape;
if((square.calculateArea())>threshold)
square.displayAttributes();
}
}
}
}
我提供的示例中使用抽象类的目的是为不同类型的形状(如圆形和矩形)创建公共基础,以便您可以定义公共行为并确保所有形状类的结构一致。这就是使用抽象类的原因: 您可以实现其他方法,例如十进制格式化程序。
package AbstractClasses;
abstract class ShapesStack {
abstract double calculateArea();
abstract double calculatePerimeter();
abstract void displayAttributes();
}
class Circles extends ShapesStack{
private double radius;
public Circles(double radius) {
this.radius = radius;
}
@Override
double calculateArea() {
return Math.PI * Math.pow(radius, 2);
}
@Override
double calculatePerimeter() {
return Math.PI * 2 * radius;
}
@Override
public void displayAttributes(){
System.out.println("Attributes " + radius);
}
}
class Rectangles extends ShapesStack{
private double width;
private double length;
public Rectangles(double width, double length) {
this.width = width;
this.length = length;
}
@Override
double calculateArea() {
return width * length;
}
@Override
double calculatePerimeter() {
return 2 * (width + length);
}
@Override
public void displayAttributes(){
System.out.println("Attributes (" + (int)width + ", " + (int)length + ")");
}
}
package AbstractClasses;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class StackOverFlowProblemMain {
public static void main(String[] args) {
Scanner sn = new Scanner(System.in);
List<ShapesStack> shapes = new ArrayList<>();
System.out.print("Enter the number of shapes: ");
int numOfShapes = sn.nextInt();
for (int i = 0; i < numOfShapes; i++) {
System.out.println("\nEnter 1 for circle, 2 for rectangle: ");
int choice = sn.nextInt();
if (choice == 1){
System.out.println("Enter radius of the circle: ");
double radius = sn.nextDouble();
Circles circle = new Circles(radius);
System.out.println("The area is: " + circle.calculateArea());
System.out.println("The perimeter is: " + circle.calculatePerimeter());
shapes.add(new Circles(radius));
} else if (choice == 2) {
System.out.println("Enter width: ");
double width = sn.nextDouble();
System.out.println("Enter length: ");
double length = sn.nextDouble();
Rectangles rectangles = new Rectangles(width, length);
System.out.println("The area is: " + rectangles.calculateArea());
System.out.println("The perimeter is: " + rectangles.calculatePerimeter());
shapes.add(new Rectangles(width, length));
}else {
System.out.println("Invalid choice, try again");
}
}
System.out.println("Enter the Threshold: ");
double threshold = sn.nextDouble();
for (ShapesStack shape : shapes){
double area = shape.calculateArea();
double perimeter = shape.calculatePerimeter();
if (area > threshold){
shape.displayAttributes();
System.out.println("The shape with area " + area + " and perimeter " + perimeter + ": " + shape.getClass().getSimpleName());
}
}
}
}