Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Rolling a Die and giving percent of odd and even with user input

$
0
0
import java.util.Scanner;  // for reading input data
/** Java application that simulates the rolling of a die.
**
**  The user enters, in response to a prompt, the number of times that the
**  die is to be rolled.  The die is rolled that many times, with each
**  roll's result being reported (by the display of a number between 1 and 6).
**  The program then displays a message indicating the percentage of rolls
**  that resulted in and odd value, and a message indicating the percentage
**  of rolls that resulted in an even value.
*/
   public class DieApplication {
   
      public static void main(String[] args) {
      // Establish Scanner object to interpret input entered at keyboard.
         Scanner keyboard = new Scanner(System.in);
      
// Prompt user to enter input regarding # of rolls to make
         System.out.print("Enter # times to roll: ");
      
// Read user's response and store in a variable
         int numRolls = keyboard.nextInt();
      
// Create a Die object and store a reference to it in a variable.
/* code needed here */
// Display message introducing output describing rolls of die.
         System.out.print("Results: ");
      
// Roll the die object the # of times specified by the user,
// reporting the result of each roll.
         for (int i=0; i != numRolls; i=i+1) {
            // Roll the die.
/* code needed here */
         
            // Display the result.
            /* code needed here; modify the following statement */
            System.out.print("?");
         }
      
// Skip to next line;
         System.out.println();
      
// Calculate and report percentage of rolls that resulted in an odd number
/* code needed here */
// Calculate and report percentage of rolls that resulted in an odd number
/* code needed here */
}
   
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>