Reputation: 123
I don't get what does "unreachable code" means ?
Here in the last line of my code double probabilityOfWin = wins / (wins + loses);
it says unreachable code.
import java.util.Random;
public class CrapsGame {
public static final int GAMES = 9999;
public static void main(String[] args) {
Random randomGenerator1 = new Random();
Random randomGenerator2 = new Random();
Random randomGenerator3 = new Random();
Random randomGenerator4 = new Random();
int dice1 = randomGenerator1.nextInt(6) + 1;
int dice2 = randomGenerator2.nextInt(6) + 1;
int comeoutSum = dice1 + dice2;
int point = 0;
// The comeout roll
if (comeoutSum == 7 || comeoutSum == 12)
System.out.println("wins");
else if ( comeoutSum == 2 || comeoutSum == 3 || comeoutSum == 12)
System.out.println("loses");
else
point = comeoutSum;
int wins = 0;
int loses = 0;
while(GAMES <= 9999)
{
dice1 = randomGenerator3.nextInt(6) + 1;
dice2 = randomGenerator4.nextInt(6) + 1;
int sum = dice1 + dice2;
if (sum == point)
wins++;
else if (sum == 7)
loses++;
}
double probabilityOfWin = wins / (wins + loses);
}
}
Upvotes: 0
Views: 205
Reputation: 6437
This loop here:
while(GAMES <= 9999)
{
...
}
resolves to while (true)
because the value of GAMES
is never modified. So any code that comes after (in your case, double probabilityOfWin = wins / (wins + loses);
) is deemed unreachable.
Upvotes: 2
Reputation: 700
You did not make any change to the constant GAME
. So while
loop will never terminate. Last line of code is unreachable.
Unreachable means the code never gets executed. For example,
return 15;
int a = 12;
Then the last line of code will not get executed because the function has already returned.
Upvotes: 0