public static void main(String[] args)
{
Scanner console = new Scanner(System.in);
System.out.print("Enter the value of a: ");
int a = console.nextInt();
System.out.print("Enter the value of b: ");
int b = console.nextInt();
System.out.print("Enter the value of h: ");
int h = console.nextInt();
double area = (a + b) * h / 2;
System.out.println("Area = " + "(" + a + " + " + b + ")" + " * " + h + " / " + 2 );
System.out.println("Area = " + area);
if ( a > 0 || b > 0 || h > 0)
break;
int count = 0;
for (; n = area; n++)
{
count++;
}
System.out.println("The number of trapezium calculated: " + count);
System.out.println("End.");
}
How do I use scanner for this code and calculate & print the area of the trapezium as many time until the user key in a non-positive input.
1
Expert's answer
2011-09-01T09:29:43-0400
public static void main(String[] args) { Scanner console = new Scanner(System.in); int count = 0; while(true) { System.out.print("Enter the value of a: "); int a = console.nextInt(); if (a <= 0) break; System.out.print("Enter the value of b: "); int b = console.nextInt(); if (b <= 0) break; System.out.print("Enter the value of h: "); int h = console.nextInt(); if (h <= 0) break; double area = (a + b) * h / 2; count++; System.out.println("Area = " + "(" + a + " + " + b + ")" + " * " + h + " / " + 2 ); System.out.println("Area = " + area); System.out.println("The number of trapezium calculated: " + count); System.out.println("End."); } }
Numbers and figures are an essential part of our world, necessary for almost everything we do every day. As important…
APPROVED BY CLIENTS
"assignmentexpert.com" is professional group of people in Math subjects! They did assignments in very high level of mathematical modelling in the best quality. Thanks a lot
Comments
Leave a comment