我正在尝试创建一个代码,询问用户要处理的形状数量,并询问用户是矩形,正方形还是圆形。一旦有了它,它将打印出面积和周长。
我还没有使它起作用,因为一旦它询问用户形状的数量,它只是打印出数据而没有询问长度,宽度或半径。
该代码分为两类:一类用于输入和输出,另一类用于计算。
这是进行计算的类
package edu.uic.ids.model;
import java.text.NumberFormat;
public class Shape {
//inputs
private double length;
private double width;
private double radius;
String command;
//outputs
private double area;
private double perimeter;
private String areaString;
private String perimeterString;
public Shape() {
// TODO Auto-generated constructor stub
length = 0.0;
width = 0.0;
radius = 0.0;
command = "";
area = 0.0;
perimeter = 0.0;
areaString = "";
perimeterString = "";
}
public Shape(double length, double width, double radius, String command, double area, double perimeter,
String areaString, String perimeterString) {
super();
this.length = length;
this.width = width;
this.radius = radius;
this.command = command;
this.area = area;
this.perimeter = perimeter;
this.areaString = areaString;
this.perimeterString = perimeterString;
}
public void computeShape() {
if (command.equalsIgnoreCase("R")) {
area = length * width;
perimeter = 2 * (length + width);
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(3);
nf.setMinimumFractionDigits(3);
areaString = nf.format(area);
perimeterString = nf.format(perimeter);
}
else if (command.equalsIgnoreCase("C")) {
area = Math.PI * Math.pow(radius, 2);
perimeter = 2 * Math.PI * radius;
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(3);
nf.setMinimumFractionDigits(3);
areaString = nf.format(area);
perimeterString = nf.format(perimeter);
}
else if (command.equalsIgnoreCase("S")) {
area = length * width;
perimeter = 4 * length;
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(3);
nf.setMinimumFractionDigits(3);
areaString = nf.format(area);
perimeterString = nf.format(perimeter);
}
}
public String getCommand() {
return command;
}
public void setCommand(String command) {
this.command = command;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getArea() {
return area;
}
public double getPerimeter() {
return perimeter;
}
public String getAreaString() {
return areaString;
}
public String getPerimeterString() {
return perimeterString;
}
}
这是输入和输出的代码
package edu.uic.ids.console;
import java.util.Scanner;
import edu.uic.ids.model.Shape;
public class ShapeDriver {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
Shape shape = null;
double length = 0.0;
double width = 0.0;
double radius = 0.0;
System.out.println("IDS401 Assignment Number 5 - Shapes Calculator");
System.out.println("Enter maximum number of shapes: ");
int maxNumberShapes = scanner.nextInt();
Shape shapeArray [] = new Shape [maxNumberShapes];
int shapeNumber = 0;
while(true) {
shape = new Shape();
System.out.println();
System.out.println("Enter Type of Shape (S for Square, R for rectangle, and C for Circle): ");
shape.setCommand(scanner.nextLine());
String command = shape.getCommand();
if (command.equalsIgnoreCase("R")) {
System.out.println(
"Enter Length: " +
"Enter Width: ");
length = scanner.nextDouble();
width = scanner.nextDouble();
shape.setLength(length);
shape.setWidth(width);
}
else if (command.equalsIgnoreCase("S")) {
System.out.println(
"Enter Length: " +
"Enter Width: ");
length = scanner.nextDouble();
width = scanner.nextDouble();
shape.setLength(length);
shape.setWidth(width);
}
else if (command.equalsIgnoreCase("C")) {
System.out.println(
"Enter Radius: ");
radius = scanner.nextDouble();
shape.setRadius(radius);
}
shape.computeShape();
System.out.println(
"\n" +
"Length: " + shape.getLength() + "\n" +
"Width: " + shape.getWidth() + "\n" +
"Radius: " + shape.getRadius() + "\n" +
"Area: " + shape.getAreaString() + "\n" +
"Perimeter: " + shape.getPerimeter() + "/n\n");
shapeArray[shapeNumber++] = shape;
System.out.println("Continue (y or n): ");
if(scanner.next().equalsIgnoreCase("n") || maxNumberShapes == shapeNumber) {
break;
}
}
System.out.println("shapeNumber\tlength\twidth\tradius\tarea\tperimeter\n");
for(int i = 0; i < maxNumberShapes; i++) {
if (shapeArray[i] == null)
continue;
shape = shapeArray[i];
System.out.println("\t" + i + "\t" +
shape.getLength() + "\t" +
shape.getWidth() + "\t\t" +
shape.getRadius() + "\t+t" +
shape.getAreaString() + "\t" +
shape.getPerimeterString());
}
System.out.println("\nShape app exit\n");
scanner.close();
}
}
任何帮助都会很棒