Guest User

Untitled

a guest
Jan 22nd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package randomno;
  6.  
  7. import java.util.Scanner;
  8.  
  9. /**
  10. *
  11. * @author David
  12. */
  13. public class Randomno {
  14.  
  15. /**
  16. * @param args the command line arguments
  17. */
  18. static boolean isRunning = true;
  19.  
  20. public static void main(String[] args) {
  21. // TODO code application logic here
  22. Scanner s = new Scanner (System.in);
  23. String input;
  24. int guess;
  25. while (isRunning)
  26. {
  27. int answer = (int)(100 * Math.random());
  28. System.out.println("Guess any between 1 and 100 or type \"Quit\" to exit:");
  29. while (true)
  30. {
  31. input = s.nextLine();
  32. if (input.toLowerCase().equals("quit") || input.toLowerCase().equals("exit"))
  33. {
  34. isRunning = false;
  35. break;
  36. }
  37. try
  38. {
  39. guess = Integer.decode(input);
  40. if (guess == answer)
  41. {
  42. System.out.println("You Win");
  43. System.out.println("Play again?");
  44. input = s.nextLine();
  45. if (input.toLowerCase().equals("quit") || input.toLowerCase().equals("exit") || input.toLowerCase().equals("no") || input.toLowerCase().equals("n"))
  46. {
  47. isRunning = false;
  48. break;
  49. }
  50. else if (input.toLowerCase().equals("yes") || input.toLowerCase().equals("y"))
  51. {
  52. break;
  53. }
  54. else
  55. {
  56. System.out.println("Invalid response, so then yes.");
  57. break;
  58. }
  59. }
  60. else if (guess < answer)
  61. {
  62. System.out.println("Higher Than That...");
  63. }
  64. else
  65. {
  66. System.out.println("Lower Than That...");
  67. }
  68. }
  69. catch (NumberFormatException e)
  70. {
  71. System.out.println("Not a number! Try again!");
  72. }
  73. }
  74.  
  75. }
  76. }
  77. }
Add Comment
Please, Sign In to add comment