- Joined
- May 12, 2020
- Messages
- 2,020
- Points
- 147
- IGN
- UntilNightfall
Idk the indexing is weird
//Making a new package
package Example;
//Importing the java utility "Scanner".
import java.util.Scanner;
//Importing the java utility "Random".
import java.util.Random;
//Making a new class.
public class NumberGame {
public static void main(String[] args) {
//Making a new random object called "rand".
Random rand= new Random();
//Making a new scanner object called "sc".
Scanner sc = new Scanner(System.in);
//Making a new integer called tries and setting it to 0.
int tries = 0;
//Making a new integer called win and setting it to 0.
int win = 0;
//Asking the user for the maximum number.
System.out.println("What is the maximum number for the guessing game?");
//Creating an input for the user's answer.
int max = sc.nextInt();
//Making a new random number.
int random_number = rand.nextInt(max);
//Making a while loop and making it run as long as win is equal to 0.
while(win == 0){
//Asking the user to guess a number from 0 to the maximum input.
System.out.println("Guess a number from 0 to " + max+ " (including 0).");
//Making a new input for the user's guess.
int guess = sc.nextInt();
//Making a condition if the input is less than the random number.
if(guess < random_number) {
//Letting the user know that the input was too low.
System.out.println("Your guess was too low, guess a number higher than " + guess);
//Adding 1 to the integer "tries".
tries++;
}
//Making a condition if the input is greater than the random number.
if(guess > random_number) {
//Letting the user know that the input was too high.
System.out.println("Your guess was too high, guess a number lower than " + guess);
//Adding 1 to the integer "tries".
tries++;
}
//Making a condition if the input is equal to the random number.
if(guess == random_number) {
//Letting the user know that the input was right.
System.out.println("You guessed the number!");
//Adding 1 to the integer "tries".
tries++;
//Letting the user know that he/she took how many tries before they got it right.
System.out.println("It took you " + tries + " tries to guess the number.");
//Adding 1 to the integer "win".
win++;
}
}
}
}
//Making a new package
package Example;
//Importing the java utility "Scanner".
import java.util.Scanner;
//Importing the java utility "Random".
import java.util.Random;
//Making a new class.
public class NumberGame {
public static void main(String[] args) {
//Making a new random object called "rand".
Random rand= new Random();
//Making a new scanner object called "sc".
Scanner sc = new Scanner(System.in);
//Making a new integer called tries and setting it to 0.
int tries = 0;
//Making a new integer called win and setting it to 0.
int win = 0;
//Asking the user for the maximum number.
System.out.println("What is the maximum number for the guessing game?");
//Creating an input for the user's answer.
int max = sc.nextInt();
//Making a new random number.
int random_number = rand.nextInt(max);
//Making a while loop and making it run as long as win is equal to 0.
while(win == 0){
//Asking the user to guess a number from 0 to the maximum input.
System.out.println("Guess a number from 0 to " + max+ " (including 0).");
//Making a new input for the user's guess.
int guess = sc.nextInt();
//Making a condition if the input is less than the random number.
if(guess < random_number) {
//Letting the user know that the input was too low.
System.out.println("Your guess was too low, guess a number higher than " + guess);
//Adding 1 to the integer "tries".
tries++;
}
//Making a condition if the input is greater than the random number.
if(guess > random_number) {
//Letting the user know that the input was too high.
System.out.println("Your guess was too high, guess a number lower than " + guess);
//Adding 1 to the integer "tries".
tries++;
}
//Making a condition if the input is equal to the random number.
if(guess == random_number) {
//Letting the user know that the input was right.
System.out.println("You guessed the number!");
//Adding 1 to the integer "tries".
tries++;
//Letting the user know that he/she took how many tries before they got it right.
System.out.println("It took you " + tries + " tries to guess the number.");
//Adding 1 to the integer "win".
win++;
}
}
}
}