Skip to content

Instantly share code, notes, and snippets.

@CaptainHandyman
Created June 8, 2021 13:44
Show Gist options
  • Save CaptainHandyman/4ce290af872ec7687fd2b6bb191517c3 to your computer and use it in GitHub Desktop.
Save CaptainHandyman/4ce290af872ec7687fd2b6bb191517c3 to your computer and use it in GitHub Desktop.
A game written in Java where you have to hit a number.
package org.redbull4.helloworld;
import java.util.Scanner;
public class Hello {
static Scanner sc = new Scanner(System.in);
public static int getRandomNumber(final int a, final int b) {
double output = Math.random() * ((b - a - 1) + a);
return (int) output;
}
public static void main(String[] args) {
while (true) {
System.out.print("Suit a number from 1 to 10: ");
if (sc.nextInt() == getRandomNumber(1, 10)) {
System.out.println("That's right, you hit the number!");
break;
} else
System.out.println("Wrong, try again!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment