Guest User

Untitled

a guest
Sep 21st, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class HelloWorld
  4. {
  5. public static void main(final String...args)
  6. {
  7. Scanner scanner = new Scanner(System.in);
  8. System.out.println("What was your name again? ");
  9. String name = scanner.next();
  10. System.out.println("Hey Ray, can you give me a random number > 0? ");
  11.  
  12. int randomNum = 0;
  13.  
  14. Random random = new Random();
  15. int tries = 0;
  16. while(randomNum <= 0)
  17. {
  18. String number = scanner.next();
  19. try
  20. {
  21. randomNum = Integer.parseInt(number);
  22. if(randomNum <= 0)
  23. {
  24. throw new NumberFormatException("I wanted a number > 0...");
  25. }
  26. }
  27. catch (NumberFormatException e)
  28. {
  29. tries++;
  30. System.out.println("That was not a number, please try again... ");
  31.  
  32. if(tries >= 10)
  33. {
  34. randomNum = random.nextInt(10);
  35. System.out.println("Okay since you don't know what a number is, let's continue with: " + randomNum);
  36. }
  37. }
  38. }
  39.  
  40. Date date = new Date(); // given date
  41. Calendar calendar = GregorianCalendar.getInstance(); // creates a new calendar instance
  42. calendar.setTime(date); // assigns calendar to given date
  43. int hour = calendar.get(Calendar.HOUR_OF_DAY);
  44.  
  45. System.out.println("It's around: " + hour + " o'clock");
  46.  
  47. int betterRandomNumer = hour * randomNum * random.nextInt(10);
  48.  
  49. System.out.println("Our special number turned out to be: " + betterRandomNumer);
  50.  
  51. if(betterRandomNumer > 100)
  52. {
  53. System.out.println("This is just too big, we will cut it down");
  54. randomNum = randomNum/(randomNum * random.nextInt(10));
  55. System.out.println("Our new special number turned out to be: " + betterRandomNumer + " way better!");
  56. }
  57.  
  58. for(int i = 0; i < betterRandomNumer; i++)
  59. {
  60. switch(lastDigit(i))
  61. {
  62. case 1:
  63. System.out.println("Hello World");
  64. break;
  65. case 2:
  66. System.out.println("Hallo Welt");
  67. break;
  68. case 3:
  69. System.out.println("Oi Mundo");
  70. break;
  71. case 4:
  72. System.out.println("Olá Mundo");
  73. break;
  74. case 5:
  75. System.out.println("Salute Monde");
  76. break;
  77. default:
  78. System.out.println("Oh no, I'm out of languages, this will be ugly!");
  79.  
  80. }
  81. }
  82.  
  83. }
  84.  
  85. public static int lastDigit(int number) { return number % 10; }
  86. }
  87.  
  88.  
  89. Output:
  90.  
  91. What was your name again?
  92. Ray
  93. Hey Ray, can you give me a random number > 0?
  94. Hi
  95. That was not a number, please try again...
  96. Oh sorry
  97. That was not a number, please try again...
  98. That was not a number, please try again...
  99. 110
  100. It's around: 14 o'clock
  101. Our special number turned out to be: 9240
  102. This is just too big, we will cut it down
  103. Our new special number turned out to be: 16 way better!
  104. Oh no, I'm out of languages, this will be ugly!
  105. Hello World
  106. Hallo Welt
  107. Oi Mundo
  108. Olá Mundo
  109. Salute Monde
  110. Oh no, I'm out of languages, this will be ugly!
  111. Oh no, I'm out of languages, this will be ugly!
  112. Oh no, I'm out of languages, this will be ugly!
  113. Oh no, I'm out of languages, this will be ugly!
  114. Oh no, I'm out of languages, this will be ugly!
  115. Hello World
  116. Hallo Welt
  117. Oi Mundo
  118. Olá Mundo
  119. Salute Monde
Advertisement
Add Comment
Please, Sign In to add comment