Skip to content

Instantly share code, notes, and snippets.

@nasko90
Created September 27, 2016 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nasko90/bb633c6d10a328c91b56688fcbe38364 to your computer and use it in GitHub Desktop.
Save nasko90/bb633c6d10a328c91b56688fcbe38364 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class Lakes {
public static void main (String [] args){
Scanner input = new Scanner (System.in);
System.out.println("What is the structure of the lake ? Use 'd' for down , 'u' for up, and 'h' for horizontal");
String lake = input.nextLine();
String [] lakeArray = lake.split("");
double down = 0.5;
double squareDown = 0;
double horizontal = 1;
double squareHorizontal = 0;
double squareUp = 0;
double square = 0;
for (int i=0; i<lakeArray.length; i++){
if (lakeArray[i].equals("d")){
squareDown += down;
down++;
}
if (lakeArray[i].equals("h")){
squareHorizontal += horizontal*(down-0.5);
}
if (lakeArray[i].equals("u")){
down--;
squareUp += down;
}
}
if (squareUp==squareDown){
square = (squareDown + squareUp+squareHorizontal)*1000;
int squareLake = (int) square;
System.out.println("The square of the lake is - " +squareLake);
}
else {
System.out.println("The depth must always start from 0 and finish at depth 0 ('d' and 'u' must be equal)!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment