Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.Scanner;
- public class Josephus {
- public static void main (String [] args){
- Scanner input = new Scanner(System.in);
- Random random = new Random();
- String name;
- int position;
- int execute = 0;
- int killNumber;
- int deathNumber = 0;
- int survivors = 0;
- int counter = 0;
- int number = random.nextInt(100);
- if (number < 10)
- number = 10;
- System.out.println("What is your name?");
- name = input.next();
- System.out.printf("%s, you are among a group of people who are going to be executed.\n", name);
- System.out.println("Only one will live.");
- System.out.printf("There are %d people who are going to be executed with you.\n", number);
- System.out.println("Please enter a kill number.");
- killNumber = input.nextInt();
- System.out.printf("The kill number is %d.\n", killNumber);
- System.out.println("You are being assigned a random position.");
- System.out.println(".\n.\n.\n");
- position = random.nextInt(number);
- System.out.printf("You have been placed at position number %d.\n", position);
- System.out.printf("Good luck, %s.\n", name);
- System.out.println();
- boolean[] array = new boolean[number];
- for (counter = 0; counter < array.length; counter++){
- array[counter] = true;
- if (counter == position){
- System.out.printf("Person %d (YOU): ", counter);
- System.out.print(array[counter]);
- System.out.println();}
- else{
- System.out.printf("Person %d: ", counter);
- System.out.print(array[counter]);
- System.out.println();}
- }
- System.out.println();
- System.out.println("Let the execution begin.");
- System.out.println();
- counter = 0;
- survivors = number;
- for (counter = 0; counter < array.length; counter++){
- while (survivors != 1){
- execute += killNumber;
- if (execute >= number){
- execute -= number;
- }
- if (array[execute] = true){
- array[execute] = false;
- survivors--;
- if (execute == position){
- System.out.println("YOU DIED!");
- }
- else {
- deathNumber++;
- }
- System.out.println();
- System.out.printf("Person %d has been executed.\n", execute);
- }
- else {
- execute++;
- }
- }
- counter = 0;
- for (counter = 0; counter < array.length; counter++){
- if (counter == position){
- System.out.printf("Person %d (YOU): ", counter);
- System.out.print(array[counter]);
- System.out.println();}
- else{
- System.out.printf("Person %d: ", counter);
- System.out.print(array[counter]);
- System.out.println();}
- }
- counter = 0;
- for (counter = 0; counter < array.length; counter++){
- if (array[counter] = true){
- if (counter == position){
- System.out.println();
- System.out.printf("Congratulations, %s, you survived!\n", name);
- }
- else {
- System.out.println();
- System.out.printf("Person %d has survived.\n", counter);
- }
- }
- System.out.println();
- System.out.printf("In order of death, you were number %d.\n", deathNumber);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment