【发布时间】:2014-09-30 19:24:40
【问题描述】:
这与我之前的问题有关,可以在以下位置找到:
Math equation result loses decimals when displayed
在我的作业中,我们必须计算等腰梯形的周长。周长需要格式化为 4 位小数。如果之后的结果 小数位全为零,则不显示零。 (例如:结果是 12.000000 什么 将显示为 12。)此外,如果结果在小数点前大于 1000,则 必须显示逗号。 (示例:结果为 1234.56781 将显示的内容是 1,234.5678)。我们需要使用十进制格式类。这是我的代码:
//Kyle Collins
/*This program calculates the area and perimeter of an isosceles trapezoid, as well
as the diagonal of the isosceles trapezoid.
*/
import java.util.Scanner;
import java.lang.Math;
import java.text.*;
public class CSCD210Lab2
{
public static void main (String [] args)
{
Scanner mathInput = new Scanner(System.in);
//declare variables
double topLength, bottomLength, height,perimPt1,perimPt2;
//Get user input
System.out.print("Please Enter Length of the Top of Isosceles Trapezoid: ") ;
topLength = mathInput.nextDouble() ;
mathInput.nextLine() ;
System.out.print("Please Enter Length of the Bottom of Isosceles Trapezoid: ") ;
bottomLength = mathInput.nextDouble() ;
mathInput.nextLine() ;
System.out.print("Please Enter Height of Isosceles Trapezoid: ") ;
height = mathInput.nextDouble() ;
mathInput.nextLine() ;
perimPt1 = ((bottomLength - topLength)/2);
perimPt2 =(Math.sqrt(Math.pow(perimPt1,2) + Math.pow(height,2)));
double trapArea = ((topLength + bottomLength)/2*(height));
double trapDiag = (Math.sqrt(topLength*bottomLength + Math.pow(height,2)));
double trapPerim = 2*(perimPt2) + (topLength + bottomLength);
//Print the results
System.out.println();
System.out.println("The Area of the Isosceles Trapezoid is: "+trapArea);
System.out.printf("The Diagonal of the isosceles trapezoid is: %-10.3f%n",trapDiag);
System.out.printf("The Perimeter of the Isosceles Trapezoid is: "+trapPerim );
}
}
我将如何格式化周边的打印输出,以便它使用十进制格式类并满足要求?
【问题讨论】:
标签: java class decimalformat