Answers for "how to create a guess my number with java"

0

guess the number java

import java.util.Scanner;
import java.util.Random;

public class GuessingGame{

    public static void main(String[] args) {

        int random, guess, attempts;
        Scanner keyboard = new Scanner(System.in);
        Random generator = new Random();
        random = generator.nextInt(100) + 1;
        attempts = 1; 

        System.out.print("I am thinking of a number between 0 and 100, what do you think it is?");

        guess = keyboard.nextInt(); 
        while (guess != random) {
            if (guess > random) {
                System.out.print("Lower!");
                attempts += 1; 
            }
            else {
                System.out.print("Higher!");
                attempts +=1;
            } 
        }

        System.out.print(random + "is the correct answer and it took you" + attempts + "attempts to guess it!");

    }        
}
Posted by: Guest on July-02-2020

Code answers related to "how to create a guess my number with java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language