In: Computer Science
Write a method that takes the sides of a dice and produces a
random number between 1 and the value of the dice sides.
use the following header:
public static int roll(int diceSides)
Add a main method to the class that tests the method by prompting
for a int for dice sides and a color for the die,
invoking the method and printing the returned number and the color
of the die.
SAMPLE RUN #1: java DiceRoll
Enter a number of sides: 20↵
Enter a color for the die: Red↵
The Red die rolled a 13↵
import java.util.*;
class A {
public static int roll(int diceSides){
Random rand = new Random();
int n =
rand.nextInt(diceSides)+1;
return n;
}
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
System.out.print("Enter a number of
sides: ");
int n = sc.nextInt();
System.out.print("Enter a color for
the die: ");
String s = sc.next();
System.out.println("The "+s+" die
rolled a "+roll(n));
}
}
File Edit View Search Terminal Help nikhil@nikhil-Vostro-15-3568:-/Desktop/CODE$ javac A.java nikhil@nikhil-Vostro-15-3568:-/Desktop/CODE$ java A Enter a number of sides: 20 Enter a color for the die: Red The Red die rolled a 6 nikhil@nikhil-Vostro-15-3568:-/Desktop/CODES |
~/Desktop/CODE/A.java (CODE) - Sublime Text (UNREGISTERED), File Edit Selection Find View Goto Tools Project Preferences Help FOLD A.java * demo. cx demo.cpp * d.cpp x index.html * import java.util.*; class A { * public static int roll(int diceSides) { Random rand = new Random(); int n = rand.nextInt(diceSides); return n; public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number of sides: "); int n = sc.nextInt(); System.out.print("Enter a color for the die: "); String S = sc.next(); System.out.println("The "+5+" die rolled a "+roll(n)); 18 19 1 1 1 N N N N N Line 19, Column 2 Tab Size: 4 Java
THANK YOU!! PLEASE VOTE