사용자가 등변면 사다리꼴의 면적을 계산할 수있는 클래스 용 프로그램을 작성 중입니다. 그것은 13.5의 답변 발생한다 때 나는, 12.0의 답변을 얻을 것이다, 높이수학 방정식 결과가 표시 될 때 소수점 이하 자릿수가 사라짐
import java.util.Scanner;
import java.lang.Math;
public class CSCD210Lab2
{
public static void main (String [] args)
{
Scanner mathInput = new Scanner(System.in);
//declare variables
int topLength, bottomLength, height;
//Get user input
System.out.print("Please enter length of the top of isosceles trapezoid: ") ;
topLength = mathInput.nextInt() ;
mathInput.nextLine() ;
System.out.print("Please enter length of the bottom of isosceles trapezoid: ") ;
bottomLength = mathInput.nextInt() ;
mathInput.nextLine() ;
System.out.print("Please enter height of Isosceles trapezoid: ") ;
height = mathInput.nextInt() ;
mathInput.nextLine() ;
double trapArea = ((topLength + bottomLength)/2*(height));
System.out.println();
System.out.printf("The area of the isosceles trapezoid is: "+trapArea);
}
}
내가 말할 입력하면, 2 topLength를 들어, bottomLength 7, 3 : 여기 내 코드입니다. 아무도 내 코드가 잘못된 대답을 인쇄하고 .5를 인쇄하지 않는 이유를 알고 있습니까?
이러한 간단한 수정, 나는 거의도에 대한 바보가 된 기분 질문. 고마워요! – Kjc21793
StackOverflow를 살펴보면 Integer Division 문제에 얽매여있는 사람들이 꽤 있습니다. 이제 알았어! 다행스럽게 도울 수있어! (당신이하고 싶은 것에 따라 업데이트를 보아라 - 더 쉬운 수정들) – blo0p3r