Bill Myte
Bill Myte

Reputation: 11

Java boolean if question

I am making a lottery program where I am asking if basically they would like a quick pick ticket. The numbers for their ticket of course would be random since it is a quick pick but the first four numbers range from 0-9 while the fifth number only goes up to 0-4. I am trying to ask them to input a button such as either "1" for no or "2" for yes if they don't want one then it would skip this step. But I am doing the boolean part incorrectly though. Could someone help me out?

Here is an example

System.out.println("Do you want Quick pick, 1 for no or 2 for yes?  The first four numbers is from a separate set of 0 to 9 and the fifth number is from a set of 0 to 4.");
QuickPick=keyboard.nextInt();
if((QuickPick==1)){
    return false;
}
if((QuickPick==2)){
    return true;
    int n = (int)(Math.random()*9+0);
System.out.println("Your QuickPick numbers are: " + kickerNumbers + kickerPowerball);
}

I still haven't gotten around to making the line of code for the final number of 0-4, just the first four numbers, so I haven't forgotten that.

Upvotes: 0

Views: 2181

Answers (4)

Sourav Purakayastha
Sourav Purakayastha

Reputation: 775

if((QuickPick==2)){ return true; int n = (int)(Math.random()*9+0); System.out.println("Your QuickPick numbers are: " + kickerNumbers + kickerPowerball); }

In the above copied code from your question, I see that you will be getting compilation errors in your IDE. Your IDE will complain about "Unreachable Code" for the line that is just below the return statement. So, you need to put the return statement at the end of the if block.

Upvotes: 0

tsm
tsm

Reputation: 3658

Like Marvo said, you dropped a brace in your if.

But you also have faulty logic. I'm not quite sure what the purpose of the method you're in is (that returns a boolean value). But your last few lines will never be reached unless the user types in something like 3 or 42.

Assuming the method is supposed to a) Ask if the user wants a Quick Pick b) Calculate the Quick Pick, if desired c) Return true/false depending on whether the Quick Pick happened or not, you should have:

public boolean doQuickPick()
{
    System.out.println("Do you want Quick pick, 1 for no or 2 for yes?  The first four numbers is from a separate set of 0 to 9 and the fifth number is from a set of 0 to 4.");
    QuickPick=keyboard.nextInt();
    if((QuickPick==1)){
        return false;
    }
    if((QuickPick==2)){
        int n = (int)(Math.random()*9+0);
        System.out.println("Your QuickPick numbers are: " + kickerNumbers + kickerPowerball);
        return true;
    }
}

As a separate issue, it'd be much better style to break that into several methods. boolean yesNoPrompt(String message), generateQuickPick(), etc.

Your question is kind of unclear, so I'm afraid I can't be much more help than that. Do post any clarifications / further questions if you have them.

Upvotes: 0

Kaffiene
Kaffiene

Reputation: 713

The code after return true will not be executed - you need to put that prior to the return statement

Upvotes: 1

debracey
debracey

Reputation: 6607

Your code for case 2 immediately does a return true; which ends the method (I assume this is in a method) right then and there. Your other lines don't get execute at all.

Consider using a switch() statement here, it'll make it easier to read:

switch(QuickPick)
{
   case 1:
      return false;
   case 2:
      int n = (int)(Math.random()*9+0); // Why is n here? You don't do anything with it?
      System.out.println("Your QuickPick numbers are: " + kickerNumbers + kickerPowerball);
      return true;
   default:
      // Uh oh - someone did something bad maybe just return false?
      return false; 
}

Also your code for case 2 is definitely wrong, you need to generate a total of five numbers, using bounds 0-9 for the first 4 and 0-4 for the last one. You'll want to use Java's Random to do this (not Math.Random) something like:

Random rand = new Random();
int somethingRandom = rand.nextInt(10); 
// Will give you an integer value where 0 < val < 10
// You can call rand.nextInt as many times as you want

To avoid doing your homework for you -- I'll follow the typical CS textbook line and say "Implementation left as an exercise."

Upvotes: 2

Related Questions