Created
April 9, 2018 16:47
-
-
Save Ge0rg3/16a071bf95a4451b6996f566492399e3 to your computer and use it in GitHub Desktop.
My first Java program <--
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
import java.util.Random; | |
import java.util.Scanner; | |
public class NumberGuessingGame { | |
public static void main(String[] args) { | |
Random rand = new Random(); | |
int value = rand.nextInt(101); | |
int userguess = 0; | |
int tries = 0; | |
System.out.println("I'm thinking of a number..."); | |
while (userguess!=value) { | |
Scanner userInput = new Scanner(System.in); | |
if (userInput.hasNextInt()) { | |
userguess = userInput.nextInt(); | |
tries++; | |
if (userguess != value) { | |
int dist = (userguess > value)? 1:2; | |
switch(dist) { | |
case 1: System.out.println("Too high!");break; | |
case 2: System.out.println("Too low!");break; | |
default: break; | |
} | |
} | |
} else { | |
System.out.println("Wrong data format..."); | |
} | |
} | |
System.out.println("Well done! This took you "+tries+" attempts."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment