waltermilner

Untitled

Oct 3rd, 2018
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.59 KB | None | 0 0
  1.  
  2. import java.util.Scanner;
  3.  
  4. public class Test {
  5.  
  6.   public static void main(String[] args) {
  7.  
  8.     Scanner scan = new Scanner(System.in); // nurodoma, kad Scanner klase naudotu klaviatura kaip ivesti ir sukurtu objekta scan
  9.     int theNumber = (int) (Math.random() * 100 + 1); // sukuriamas atsitiktinis skaicius spejimui      
  10.     System.out.println(theNumber);
  11.     int guess = 0; // sukuriamas kintamasis, kuri nustatys vartotojas klaviaturos pagalba    
  12.     String inputLine; // whatever they type in
  13.     while (guess != theNumber) { // algoritmas kartosis kol skaicius bus atspetas          
  14.       System.out.println("Atspėk skaičių nuo 1 iki 100:"); // vartotojas informuojamas, jog gali speti skaiciu  
  15.       boolean failed = true; // have they failed to input an int?
  16.       while (failed) { // repeat if input wrong
  17.         try {
  18.           inputLine = scan.nextLine(); // get whatever they type in
  19.           guess = Integer.parseInt(inputLine); // try to change to an int
  20.           failed = false; // it was OK
  21.         } catch (NumberFormatException nfe) { // if not valid input
  22.           System.out.println("Invalid - please enter a whole number"); // tell them
  23.           failed = true; // they faied - it will repeat
  24.         }
  25.       }
  26.  
  27.       if (guess < theNumber) {
  28.         System.out.println("Skaičius " + guess + " yra per mažas. Mėgink dar kartą.");
  29.       } else if (guess > theNumber) {
  30.         System.out.println("Skaičius " + guess + " yra per didelis. Mėgink dar kartą.");
  31.       }
  32.     }
  33.     System.out.println("Atspėjai. Skaičius " + guess + " yra teisingas.");
  34.   }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment