Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, I\'m getting an error in java here with NaN this is my code: public class Cr

ID: 3572597 • Letter: H

Question

Hi, I'm getting an error in java here with NaN this is my code:

public class CrapsGame {

    public static void main(String[] args) {
            int dice1 = (int)(Math.random()* 6) + 1;
            int dice2 = (int)(Math.random()* 6) + 1;
            int roll = dice1 + dice2;
            System.out.println();
            System.out.print("You rolled "+roll+". ");
            if(roll == 2 || roll == 3 || roll == 12){
                    System.out.println("You Lose !");
            }else if(roll == 7 || roll == 11){
                    System.out.println("You Win !");
            }else{
                    System.out.println("Point is "+roll+" ");
                    dice1 = (int)(Math.random()* 6) + 1;
                    dice2 = (int)(Math.random()* 6) + 1;
                    int roll2 = dice1 + dice2;
                    System.out.print("You rolled "+roll2+". ");
                    while(roll2 != 7){
                            if(roll == roll2){
                                    System.out.println("You Win !");
                                    break;
                            }else{
                                    System.out.println("Point is "+roll+" ");
                            }
                            dice1 = (int)(Math.random()* 6) + 1;
                            dice2 = (int)(Math.random()* 6) + 1;
                            roll2 = dice1 + dice2;
                            System.out.print("You rolled "+roll2+". ");
                    }
                    if(roll2 == 7){
                            System.out.println("You Lose !");
                    }                     
            }
    
      // Create some variables to control the times it won and times it lost
        int timesWon = 0;
        int timesLost = 0;


        // Here we're saying it's gonna repeat 10000 times, incrementing 1 by 1
        for(int i=0; i<10000; i++) {
            // Here you will insert all the existing logic from your program

            // In the existing code, increment the variables declared according

            if(roll == 2 || roll == 3 || roll == 12){
                System.out.println("You Lose !");
                timesLost++;
            }else if(roll == 7 || roll == 11){
                System.out.println("You Win !");
                timesWon++;
            }else{
                // Where you find it won, insert `timesWon++;`
                // Where you find it lost, insert `timesLost++;`
            }
        }
        // Here it exits the for loop (after 10000 times)
        // Here's where you can calculate

        System.out.println("Probability of winning: " + ((double)timesWon/(timesWon + timesLost)));
    }
}

output only works on probability = 1 or 0 it should be a really easy fix, thank you.

Explanation / Answer

Please Find the below program with the resolved error and with the required output .

public class CrapsGame {

    public static void main(String[] args) {
            int dice1 = (int)(Math.random()* 6) + 1;
            int dice2 = (int)(Math.random()* 6) + 1;
            int roll = dice1 + dice2;
            System.out.println();
            System.out.print("You rolled "+roll+". ");
            if(roll == 2 || roll == 3 || roll == 12){
                    System.out.println("You Lose !");
            }else if(roll == 7 || roll == 11){
                    System.out.println("You Win !");
            }else{
                    System.out.println("Point is "+roll+" ");
                    dice1 = (int)(Math.random()* 6) + 1;
                    dice2 = (int)(Math.random()* 6) + 1;
                    int roll2 = dice1 + dice2;
                    System.out.print("You rolled "+roll2+". ");
                    while(roll2 != 7){
                            if(roll == roll2){
                                    System.out.println("You Win !");
                                    break;
                            }else{
                                    System.out.println("Point is "+roll+" ");
                            }
                            dice1 = (int)(Math.random()* 6) + 1;
                            dice2 = (int)(Math.random()* 6) + 1;
                            roll2 = dice1 + dice2;
                            System.out.print("You rolled "+roll2+". ");
                    }
                    if(roll2 == 7){
                            System.out.println("You Lose !");
                    }                     
            }
    
  
        int timesWon = 0;
        int timesLost = 0;



        for(int i=0; i<10000; i++) {

            if(roll == 2 || roll == 3 || roll == 12){
                System.out.println("You Lose !");
                timesLost++;
            }else if(roll == 7 || roll == 11){
                System.out.println("You Win !");
                timesWon++;
            }else{
              
            }
        }

// System.out.println("Probability of winning: " + ((double)timesWon/(timesWon + timesLost)));

double x = timesWon + timesLost;

if(x!=0) {

System.out.println("Probability of winning: " + ((double)timesWon/(timesWon + timesLost)));

}else {

System.out.println("Probability of winning: " + x);

}
    }
}

// While deviding 0.0/0.0 Always it gives NotaNumber(NaN).So here timeswon and timeslost both r initialized as zero but While execution only initialized values entering to probability calculation Not entering into else case Thats why Verifying null also Before calculating. Thats it . Finally error is resolved ...:)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote