Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

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!");

    }        
}
Comment

PREVIOUS NEXT
Code Example
Java :: foreground java jframe 
Java :: set fontcolor of jframe 
Java :: read int from keyboard java 
Java :: moving text in textview android 
Java :: spring boot resource optional request param 
Java :: finding absolute value in java 
Java :: android studio visibility 
Java :: cannot lock java compile cache as it has already been locked by this process 
Java :: java split string into list 
Java :: java servlets session call 
Java :: processing draw circle 
Java :: libgdx move camera with touch 
Java :: java stream collect to string 
Java :: from string to double andoird studio 
Java :: calculate pi in java 
Java :: hibernate select lowercase 
Java :: java copy list 
Java :: smaller elements on right hand side leetcode 
Java :: taking date as input in java 
Java :: inject in jsp 
Java :: dialog box in java swing 
Java :: system vs integration testing 
Java :: How to find the power of a number efficiently in Java? 
Java :: How to efficiently find the middle node of a singly linked list, in Java? 
Java :: java intercept ctrl c 
Java :: how to collect objective in java 
Java :: array ausgeben java 
Java :: android java toast 
Java :: java round double to 2 decimals 
Java :: append button java 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =