lundi 23 novembre 2015

Simple Java Guessing program. Continuing

I am just playing around with java and wanted to make a simple program where the user; me has to guess/type in the correct number until it's correct. What can I do so the program can keep running, printing out "Make another guess" until the user/me puts in the correct number. Maybe a boolean? I'm not sure. This is what I have so far.

import java.util.Scanner;

public class iftothemax {
    public static void main(String[] args) {

    int myInt = 2;

    // Create Scanner object
    Scanner input = new Scanner(System.in);

    //Output the prompt
    System.out.println("Enter a number:");

    //Wait for the user to enter a number
    int value = input.nextInt();


    if(value == myInt) {
        System.out.println("You discover me!");
    }
    else {
        //Tell them to keep guessing
        System.out.println("Not yet! You entered:" + value + " Make another guess");
        input.nextInt();

    }

    }
}

Aucun commentaire:

Enregistrer un commentaire