[error]
"ProbabilityFair2.java:26: error: variable stage1 might not have been initialized
System.out.println("You have " + stage1 + " stage one.");
^
1 error"
[/error]
I've tried searching around for information on if/else statements, and I feel like based on what I've found, there shouldn't be a problem. I've tried changing my variable names and made sure they all matched, but have been unsuccessful. I originally thought that the random variable might be causing the problem, but on another program I've made, I was able to use an if/else statement with a random variable. I'm just looking for a way to solve this problem. Thanks!
import cs1.Keyboard;
import java.util.*;
public class ProbabilityFair2
{
public static void main(String [] args)
{
System.out.println("Please choose a shell: 1, 2, or 3: ");
int shell;
shell = Keyboard.readInt();
System.out.println("You have chosen shell: " + shell);
System.out.println("You have decided to keep the shell you chose.");
Random generator = new Random();
int rnumber = generator.nextInt(3)+1;
String stage1;
if (rnumber >= 2)
{stage1 = "lost";}
else if (rnumber <= 1)
{stage1 = "won";}
System.out.println("You have " + stage1 + " stage one.");
}}

New Topic/Question
Reply



MultiQuote





|