Created
February 6, 2015 09:49
-
-
Save vladholubiev/cee225381b7d2ef4da89 to your computer and use it in GitHub Desktop.
Enter your approximated PI number, desired precision and find out if you guessed well.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.company; | |
| import java.io.BufferedReader; | |
| import java.io.IOException; | |
| import java.io.InputStreamReader; | |
| public class Main { | |
| private static BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in)); | |
| private static String fullPi = String.valueOf(Math.PI); | |
| public static void main(String[] args) throws IOException { | |
| String yourPiGuess = readPi(); | |
| int precision = readPrecision(); | |
| printResult(yourPiGuess, precision); | |
| } | |
| private static String readPi() throws IOException { | |
| System.out.println("Full PI:" + fullPi); | |
| System.out.println("Enter your PI guess: "); | |
| return consoleReader.readLine(); | |
| } | |
| private static int readPrecision() throws IOException { | |
| System.out.println("Enter your precision: "); | |
| return Integer.parseInt(consoleReader.readLine()); | |
| } | |
| private static void printResult(String yourPiGuess, int precision) { | |
| if (yourPiGuess.length() < precision) { | |
| return; | |
| } | |
| if (fullPi.substring(0, precision).equals(yourPiGuess.substring(0, precision))) { | |
| System.out.println("You are right"); | |
| } else { | |
| System.out.println("Wrong"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment