Answers for "guessing game 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
0

java guessing game

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

public class Main {
    public static void print(String text) {
        System.out.println(text);
    }
    public static void main(String[] args) {
        // Getting Random Number
        Random rand = new Random();
        Scanner scan = new Scanner(System.in);
        int n = rand.nextInt(11);
        String num = String.valueOf(n);
        //
        int g = 0;
        int gInput = 1;
        print("Guess " + gInput + ":");
        String i = scan.nextLine();
        while(true){
            g++;
            gInput++;
            if(g == 3){
                print("You Lost!\nIt was " + num);
                break;
            }else{
                if(i == num){
                    print("You Won!");
                    break;
                }else{
                    print("Nope");
                    print("Guess " + gInput + ":");
                    i = scan.nextLine();
    
                }
            }
        }
        i = scan.nextLine();
    }
}
Posted by: Guest on June-17-2021

Code answers related to "guessing game java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language