While Loop Example(Triangle Area Calculator)


 

Basic Triangle Area Calculator


import java.util.Scanner;

public class Main {
	
	public static void main(String[] args) {
		
		Scanner scan = new Scanner(System.in);
		double first, second, angle, result;
		boolean situation = true;
		while(situation) {
			System.out.println("Welcome to triangle area calculator. ");
			
			System.out.print("Enter length of first corner: ");
			first = scan.nextDouble();
			
			System.out.print("Enter length of second corner: ");
			second = scan.nextDouble();
			
			System.out.print("Enter angle between first and second vertex: ");
			
			angle = scan.nextDouble();
			angle = Math.toRadians(angle);
			
			result = first * second * Math.sin(angle) * 0.5;
			
			System.out.println("Result:" + result);
			
			while(true) {
				
				System.out.println("If you want to exit, you enter 'exit'");
				System.out.println("If you want to continue, you enter 'continue'");
				System.out.print("Response: ");
                
				String text = scan.next();
                
				if(text.equals("exit")) {
					situation = false;
					break;
				}
				else if(text.equals("continue")) {
					break;
				}
			}
		}
	}
}

Outputs


Good Bye Everyone


Image Resource: https://cdn.virtualnerd.com/thumbnails/Alg2_13_03_0004-diagram_thumb-lg.png

0 Comments