All pastes #557412 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-06-10 23:49 UTC
rendered paste body
import java.util.Random;
import java.util.*;

class roll{
   public static void main(String[]args){
   Scanner sc = new Scanner(System.in);
   Random rand = new Random(System.currentTimeMills());
   int[] dice;
   int r,k,total,temp=-1;
   while(temp<=0){ //Determines number of "dice" to be rolled
      System.out.print("Enter the number of dice to be rolled: ");
      if(sc.hasNextInt()) temp=sc.nextInt();
   }
   r=temp;
   temp=-1;
   while(temp<=0){ //Determines number of "dice" to be kept
      System.out.print("Please enter the number of dice to be kept: ");
      if(sc.hasNextInt()) temp=sc.nextInt();
      if(temp>r){ //Checks if kept is greater than rolled
         temp=-1;
         System.out.println("Dice kept greater than dice rolled. "+r+" dice will
 be rolled.");
      }
   }
   k=temp;
   dice = new Int[r];
   for(int i=0;i<r;i++){ //Generates random numbers for dice values
      dice[i]=rand.nextInt(10)+1;
      while((dice[i]%10)==0){ //Exploding loop
         dice[i]+=rand.nextInt(10)+1;
      }
   }
   sort(dice);
   for(int i=0;i<k;i++){ //Adds up the kept dice to total
      total+=dice[i];
   } //the following line is the game-specific notation of the roll result
   System.out.print("Result: "+r+"k"+k+": "+total+" ( ");
   for(int i=0;i<r;i++){ //Outputs the individual values of each dice roll
      System.out.print(dice[i]+" ");
   }
   System.out.println(")");
   }

   static void sort(int[] roll){ //Sorts the array from highest to lowest
      int length = rolls.length;
      boolean swapped=true;
      while(swapped)(
        swapped=false;
        for(int i=0;i<length-1;i++){
           if(rolls[i+1]>rolls[i]){
             int r=rolls[i+1];
             rolls[i+1]=rolls[i];
             rolls[i]=r;
             swapped=true;
           }
        }
      }
   }
}