0

Not too long ago, I created a dice program in which a dice rolls twice and the sum is added as an integer. When I first created this program, I was able to use (int)Math.addExact(int, int). That no longer works; now it gives me the error "addExact(int,int)is undefined for type Math.". Code:

public class Dice {
    // try is a reserved word, but the int named trie is a try at rolling the dice.
    static int trie;
    // try1 is the first try
    static int try1;
    // try2 is the second try
    static int try2;


    public static void main(String[] args) {
        //Print one line of output, first roll
        System.out.println("First die that comes up is: "
                + (try1 = roll()));
        //Print one line in the output, second roll
        System.out.println("Second die that comes up is: "
                + (try2 = roll()));
        //Sum of the two rolls output
        System.out.println("Total: " + ((int)(Math.addExact(try1,try2))));
    }

    // SubRoutine for a dice roll
    public static int roll() {
        // Giving value to trie, rolls random number that is at least 1 and at
        // most 6
        return trie = (int) (Math.random() * 6) + 1;
2
Contributors
1
Reply
4
Views
2 Years
Discussion Span
Last Post by stultuske
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.