├── logic.png ├── README.md ├── .gitignore ├── Input.java └── Main.java /logic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theyaxh/Guess-the-number/main/logic.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Guess-the-number 2 | just basic game of guessing the number by high/low 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | replay_pid* 25 | -------------------------------------------------------------------------------- /Input.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.InputMismatchException; 3 | 4 | public class Input { 5 | private Scanner scanner; 6 | public int userInput; 7 | 8 | public Input() { 9 | this.scanner = new Scanner(System.in); 10 | } 11 | 12 | public int getUserInput() { 13 | while (true) { 14 | try { 15 | System.out.print("Enter your guess: "); 16 | userInput = scanner.nextInt(); 17 | scanner.nextLine(); // Consume the newline character 18 | 19 | if (Double.isNaN((double) userInput) || userInput < 1 || userInput > 100) 20 | throw new IllegalArgumentException("Enter an integer between 1 and 100"); 21 | 22 | return userInput; 23 | } catch (InputMismatchException e) { 24 | System.err.println("Invalid Input! Please enter a valid integer."); 25 | scanner.nextLine(); 26 | } catch (IllegalArgumentException e) { 27 | System.err.println("Invalid Input: " + e.getMessage()); 28 | } 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | 3 | class IllegalArgumentException extends Exception { 4 | public IllegalArgumentException(String message) { 5 | super(message); 6 | } 7 | } 8 | 9 | public class Main { 10 | public static int generateRandomNumber(int maxBound) { 11 | Random random = new Random(); 12 | return random.nextInt(maxBound) + 1; 13 | } 14 | 15 | public static void main(String[] args) { 16 | Input input = new Input(); 17 | int generatedNumber = generateRandomNumber(99); 18 | int userInput; 19 | boolean guessFlag = false; 20 | 21 | while(!guessFlag) { 22 | userInput = input.getUserInput(); 23 | 24 | if(userInput == generatedNumber) { 25 | System.out.println("Your guess is correct, The number was indeed " + userInput + "!"); 26 | guessFlag = true; 27 | } else if(userInput > generatedNumber) { 28 | if(userInput - generatedNumber >= 30) { 29 | System.out.println("Your guess is too high."); 30 | } else if(userInput - generatedNumber >= 7) { 31 | System.out.println("Your guess is quite high"); 32 | } else { 33 | System.out.println("Your guess is high but very close."); 34 | } 35 | } else { // if(userInput < generatedNumber) 36 | if(generatedNumber - userInput >= 30) { 37 | System.out.println("Your guess is too low."); 38 | } else if(generatedNumber - userInput >= 7) { 39 | System.out.println("Your guess is quite low"); 40 | } else { 41 | System.out.println("Your guess is low but very close."); 42 | } 43 | } 44 | } 45 | } 46 | } --------------------------------------------------------------------------------