Házi feladat

 

Alapfejezet - Ami már eddig is nagyon hiányzott: az adatbekérés

 

Gyuricza Balázs átlagos programozási előtapasztalatokkal ötletes és kiváló kódot fabrikált "Találd ki a számot!" témakörben.

 

www.informatika-programozas.hu - Futtatható Java-kód!

 

 

 

 

 

 

 

 

import java.util.*;

public class Main {
public static void main(String[] args) {
    Random rand = new Random();
    int NumberToGuess = rand.nextInt(10);
    int tries = 0;
    Scanner input = new Scanner(System.in);
    int guess = 5;
    boolean win = false;

    while(win == false) {
        System.out.println("Guess a number between 0 and 10: ");
        guess = input.nextInt();
        tries++;
        if(guess == NumberToGuess){
            win = true;
        }
        else if(guess < NumberToGuess){
            System.out.println("Number is too low");
        }
        else if(guess > NumberToGuess) {
            System.out.println("Number is too high");
            }
        }
    System.out.println("You win");
    System.out.println("The number was " + NumberToGuess);
    System.out.println("It took " + tries + " tries");
    }
}

 

Végeredmény:

Guess a number between 0 and 10:
4
Number is too low
Guess a number between 0 and 10:
5
You Win
The number was 5
It took 2 tries