Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Test {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in); // nurodoma, kad Scanner klase naudotu klaviatura kaip ivesti ir sukurtu objekta scan
- int theNumber = (int) (Math.random() * 100 + 1); // sukuriamas atsitiktinis skaicius spejimui
- System.out.println(theNumber);
- int guess = 0; // sukuriamas kintamasis, kuri nustatys vartotojas klaviaturos pagalba
- String inputLine; // whatever they type in
- while (guess != theNumber) { // algoritmas kartosis kol skaicius bus atspetas
- System.out.println("Atspėk skaičių nuo 1 iki 100:"); // vartotojas informuojamas, jog gali speti skaiciu
- boolean failed = true; // have they failed to input an int?
- while (failed) { // repeat if input wrong
- try {
- inputLine = scan.nextLine(); // get whatever they type in
- guess = Integer.parseInt(inputLine); // try to change to an int
- failed = false; // it was OK
- } catch (NumberFormatException nfe) { // if not valid input
- System.out.println("Invalid - please enter a whole number"); // tell them
- failed = true; // they faied - it will repeat
- }
- }
- if (guess < theNumber) {
- System.out.println("Skaičius " + guess + " yra per mažas. Mėgink dar kartą.");
- } else if (guess > theNumber) {
- System.out.println("Skaičius " + guess + " yra per didelis. Mėgink dar kartą.");
- }
- }
- System.out.println("Atspėjai. Skaičius " + guess + " yra teisingas.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment