Answers for "java guessing game"

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 "java guessing game"

Browse Popular Code Answers by Language