Guest User

Untitled

a guest
Nov 2nd, 2013
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class Josephus {
  5.  
  6. public static void main (String [] args){
  7.  
  8. Scanner input = new Scanner(System.in);
  9.  
  10. Random random = new Random();
  11.  
  12. String name;
  13. int position;
  14. int execute = 0;
  15. int killNumber;
  16. int deathNumber = 0;
  17. int survivors = 0;
  18. int counter = 0;
  19. int number = random.nextInt(100);
  20. if (number < 10)
  21. number = 10;
  22.  
  23. System.out.println("What is your name?");
  24. name = input.next();
  25. System.out.printf("%s, you are among a group of people who are going to be executed.\n", name);
  26. System.out.println("Only one will live.");
  27. System.out.printf("There are %d people who are going to be executed with you.\n", number);
  28. System.out.println("Please enter a kill number.");
  29. killNumber = input.nextInt();
  30. System.out.printf("The kill number is %d.\n", killNumber);
  31. System.out.println("You are being assigned a random position.");
  32.  
  33. System.out.println(".\n.\n.\n");
  34.  
  35. position = random.nextInt(number);
  36. System.out.printf("You have been placed at position number %d.\n", position);
  37. System.out.printf("Good luck, %s.\n", name);
  38. System.out.println();
  39.  
  40. boolean[] array = new boolean[number];
  41.  
  42. for (counter = 0; counter < array.length; counter++){
  43. array[counter] = true;
  44. if (counter == position){
  45. System.out.printf("Person %d (YOU): ", counter);
  46. System.out.print(array[counter]);
  47. System.out.println();}
  48. else{
  49. System.out.printf("Person %d: ", counter);
  50. System.out.print(array[counter]);
  51. System.out.println();}
  52. }
  53.  
  54. System.out.println();
  55. System.out.println("Let the execution begin.");
  56. System.out.println();
  57. counter = 0;
  58. survivors = number;
  59.  
  60. for (counter = 0; counter < array.length; counter++){
  61. while (survivors != 1){
  62. execute += killNumber;
  63. if (execute >= number){
  64. execute -= number;
  65. }
  66. if (array[execute] = true){
  67. array[execute] = false;
  68. survivors--;
  69. if (execute == position){
  70. System.out.println("YOU DIED!");
  71. }
  72. else {
  73. deathNumber++;
  74. }
  75. System.out.println();
  76. System.out.printf("Person %d has been executed.\n", execute);
  77. }
  78. else {
  79. execute++;
  80. }
  81. }
  82.  
  83. counter = 0;
  84.  
  85. for (counter = 0; counter < array.length; counter++){
  86. if (counter == position){
  87. System.out.printf("Person %d (YOU): ", counter);
  88. System.out.print(array[counter]);
  89. System.out.println();}
  90. else{
  91. System.out.printf("Person %d: ", counter);
  92. System.out.print(array[counter]);
  93. System.out.println();}
  94. }
  95. counter = 0;
  96. for (counter = 0; counter < array.length; counter++){
  97. if (array[counter] = true){
  98. if (counter == position){
  99. System.out.println();
  100. System.out.printf("Congratulations, %s, you survived!\n", name);
  101. }
  102. else {
  103. System.out.println();
  104. System.out.printf("Person %d has survived.\n", counter);
  105. }
  106. }
  107. System.out.println();
  108. System.out.printf("In order of death, you were number %d.\n", deathNumber);
  109. }
  110. }
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment