fork download
  1. //Dice game created by Kemal 'kemkoi' Kojic 2015-08-29
  2. import java.util.Scanner;
  3. import java.util.Random;
  4. public class cantSleep {
  5. //----------How the program should work-------------------//
  6. //Create a dice program which lets the user choose between h/l (High/Low)
  7. //The dice is then rolled and depending on which number it lands on there are different
  8. //actions.
  9. // l = 1-3, h = 4-6
  10. //The user should start with a bank value of a 1000 marks.
  11. //For each win the amount of the bet should be doubled.
  12. //After a win/loss the user should be prompted again, asking if they want to play again.
  13.  
  14. //----------How to create the program --------------------//
  15. //Create a boolean variable for the loop so the user can be prompted after one run.
  16. //Create one integer for the bank value and one for the dice.
  17. //Create a char and set make it so the Scanner takes in values for either 'h' or 'l'
  18. //If the user gives any value other than the ones above prompt them to do it over again.
  19. //Create a random method which random's out the dice number.
  20. //Create an switch/case for the different outcomes on the dice number.
  21.  
  22. //------Problems encountered----//
  23. //Everything works until the last step, whenever you win/lose
  24. //the bankValue doesn't change.
  25.  
  26.  
  27. //Random number generator method.
  28. public static int randomGenerator(int startValue, int lastValue)
  29. {
  30. Random rand = new Random();
  31.  
  32. int randomNum = rand.nextInt((lastValue-startValue) + 1) + startValue;
  33. return randomNum;
  34. }
  35.  
  36. public static void diceWin(int bankValue, int bet, int dice, char choice, boolean runAgain)
  37. {
  38. Scanner userInput = new Scanner(System.in);
  39.  
  40. bankValue += bet;
  41. System.out.println("Congratulations, the dice shows " + dice + " and you just won yourself " + bet + " $.");
  42. System.out.print("Would you like to bet again? (y/n): ");
  43. choice = userInput.nextLine().charAt(0);
  44. while(choice != 'y' && choice != 'n')
  45. {
  46. System.out.println("Please use the characters 'y' or 'n' next time.");
  47. System.out.print("Would you like to bet " + bet + "? (y/n): ");
  48. choice = userInput.next().charAt(0);
  49. if(choice == 'n')
  50. {
  51. runAgain = false;
  52. }
  53. }
  54. }
  55.  
  56. public static void diceLoss(int bankValue, int bet, int dice, char choice, boolean runAgain)
  57. {
  58. Scanner userInput = new Scanner(System.in);
  59.  
  60. bankValue -= bet;
  61. System.out.println("Sorry, the dice shows " + dice + " and you just lost " + bet + " $.");
  62. System.out.print("Would you like to bet again? (y/n): ");
  63. choice = userInput.nextLine().charAt(0);
  64. while(choice != 'y' && choice != 'n')
  65. {
  66. System.out.println("Please use the characters 'y' or 'n' next time.");
  67. System.out.print("Would you like to bet " + bet + "? (y/n): ");
  68. choice = userInput.next().charAt(0);
  69. if(choice == 'n')
  70. {
  71. runAgain = false;
  72. }
  73. }
  74. }
  75.  
  76. public static void main(String[] args)
  77. {
  78. Scanner userInput = new Scanner(System.in);
  79.  
  80. //Variable declaration
  81. boolean runAgain = true, low = true;
  82. int bankValue = 1000, dice = 0, bet = 0;
  83. char choice;
  84.  
  85. //Start of the program
  86. System.out.println("Hello and welcome to the dice machine.");
  87. while(runAgain == true)
  88. {
  89. System.out.println("Your current balance is: " + bankValue + " $.");
  90. //User input, if the bet amount is bigger than the balance it gives an error message out.
  91. System.out.print("How much would you like to bet?: ");
  92. bet = userInput.nextInt();
  93. while(bet > bankValue)
  94. {
  95. System.out.print("You do not have that much money on your bank account, please try again: ");
  96. bet = userInput.nextInt();
  97. }
  98. //Prompts the user for a verification of the bet.
  99. //If anything but 'y' and 'n' is typed in it gives an error message out.
  100. //If the user types a 'y' in the program continues to run, if they type an 'n' in the program closes.
  101. System.out.print("Do you really want to bet " + bet + "? (y/n): ");
  102. choice = userInput.next().charAt(0);
  103. while(choice != 'y' && choice != 'n')
  104. {
  105. System.out.println("Please use the characters 'y' or 'n' next time.");
  106. System.out.print("Would you like to bet " + bet + "? (y/n): ");
  107. choice = userInput.next().charAt(0);
  108. if(choice == 'n')
  109. {
  110. System.exit(0);
  111. }
  112. }
  113. //Asks the user what he/she would like to put the bet on. (h/l)
  114. System.out.print("What would you like to put " + bet + " on, 'h' or 'l'? (l = 1-3, h = 4-6): ");
  115. choice = userInput.next().charAt(0);
  116. while(choice != 'h' && choice != 'l')
  117. {
  118. System.out.println("Please use the characters 'h' or 'l' next time.");
  119. System.out.print("What would you like to put " + bet + " on, 'h' or 'l'? (l = 1-3, h = 4-6): ");
  120. choice = userInput.next().charAt(0);
  121. }
  122.  
  123. //Executing the randomGenerator method.
  124. dice = randomGenerator(1, 6);
  125.  
  126. //Giving the dice numbers boolean values.
  127. switch(dice)
  128. {
  129. case 1:
  130. low = true;
  131. break;
  132. case 2:
  133. low = true;
  134. break;
  135. case 3:
  136. low = true;
  137. break;
  138. case 4:
  139. low = false;
  140. break;
  141. case 5:
  142. low = false;
  143. break;
  144. case 6:
  145. low = false;
  146. break;
  147. default:
  148. System.out.println("Error, dice gave the number " + dice);
  149. }
  150.  
  151. if((low == true && choice == 'l') || (low == false && choice == 'h'))
  152. {
  153. diceWin(bankValue, bet, dice, choice, runAgain);
  154. } else if((low == true && choice == 'h') || (low == false && choice == 'l')) {
  155. diceLoss(bankValue, bet, dice, choice, runAgain);
  156. } else {
  157. System.out.println("Something went horribly wrong, please check the source code.");
  158. }
  159.  
  160. }
  161. userInput.close();
  162.  
  163. }
  164.  
  165.  
  166.  
  167.  
  168. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:4: error: class cantSleep is public, should be declared in a file named cantSleep.java
public class cantSleep {
       ^
1 error
stdout
Standard output is empty