Skip to content

Instantly share code, notes, and snippets.

@Ge0rg3
Created April 9, 2018 16:47
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 Ge0rg3/16a071bf95a4451b6996f566492399e3 to your computer and use it in GitHub Desktop.
Save Ge0rg3/16a071bf95a4451b6996f566492399e3 to your computer and use it in GitHub Desktop.
My first Java program <--
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