fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static boolean isInt(String strNum) {
  11. return strNum.matches("-?\\d+");
  12. }
  13. public static void main (String[] args) throws java.lang.Exception
  14. {
  15. Scanner sc = new Scanner(System.in);
  16.  
  17. int luckyNumber = 3;
  18. String userInput = "";
  19. int userNumber = 0;
  20.  
  21. System.out.println("Podaj liczbę: ");
  22.  
  23. while (true) {
  24. userInput = sc.nextLine();
  25.  
  26. if (!isInt(userInput)) { // walidacja!
  27. System.out.println("Podno niepoprawna liczbe!");
  28. continue;
  29. }
  30.  
  31. userNumber = Integer.parseInt(userInput);
  32.  
  33. if (userNumber!=luckyNumber) {
  34. System.out.println("zgaduj jeszcze raz: ");
  35. }
  36. else {
  37. System.out.println("Dobrze!");
  38. break;
  39. }
  40. }
  41. }
  42. }
Success #stdin #stdout 0.06s 2184192KB
stdin
1
-7
zua lichba
4
-3
3
stdout
Podaj liczbę: 
zgaduj jeszcze raz: 
zgaduj jeszcze raz: 
Podno niepoprawna liczbe!
zgaduj jeszcze raz: 
zgaduj jeszcze raz: 
Dobrze!