Thread: Can I have an if statement within an else if statement?...

1. Member
Join Date
Jan 2011
Posts
13
Rep Power
0

Can I have an if statement within an else if statement?...

(Beginner)

This is a lottery program in which the user guesses the digits and answers questions to win prizes. Everything so far (it's not complete yet) seems to be fine with my program. The only problem is the else if statement that says "Sorry! No digits match." After that is executed the user must enter a letter (a, b, c, or d) to answer the question that asks "What is the air speed velocity...etc". Then if the user answers c or d, this should be executed, "An African or European Swallow? Your prize is a pencil...etc." (shown below)
However, when I run the program and I enter c or d, nothing happens. No error, just nothing. How can I get it to execute the statement aforementioned? I used the if statement but that didn't seem to work.. It's probably simple and I'm overlooking it but I'm really scratching my head here trying to figure it out lol.

import java.util.*;

public class Lottery
{
public static void main(String[] args)
{
System.out.println("Welcome! Today you will play lottery for a chance to win great prizes!");
Scanner keys = new Scanner(System.in);

int lotteryWin = (int)(Math.random() * 100);
System.out.println("Please enter a two-digit number guess: ");
int number = keys.nextInt();

System.out.print(lotteryWin); //Display lottery number
System.out.println(", " + number); //Also display user's guess

int lotteryWin1 = lotteryWin / 10; //First digit from lottery number
int lotteryWin2 = lotteryWin % 10; //Second digit from lottery number

int numberdigit1 = number / 10; //First digit from user guess
int numberdigit2 = number % 10; //Second digit from user guess

if (lotteryWin1 == numberdigit2 && lotteryWin2 == numberdigit1)
{
System.out.println("Congratulations! Your digits match but are not in exact order."); //Digits match but not exact order
}
else if (lotteryWin1 == numberdigit1 || lotteryWin1 == numberdigit2 || lotteryWin2 == numberdigit1 || lotteryWin2 == numberdigit2)
{
System.out.println("Congratulations! One of your digits is a match. You win a pizza!"); //At least one digit matches lottery number digits
}
else if (lotteryWin !=number)
{
System.out.println("Sorry, no digits match."); //No digits match
String a = "7";
String b = "15";
String c = "27";
String d = "40";
System.out.println("What is the air speed velocity of an unladen swallow? Enter the letter corresponding to your answer: \na.7mph \nb.15mph \nc.27mph \nd.40mph");
String letter = keys.next();
if (letter.equals(c) || letter.equals(d))
{
System.out.println("An African or European swallow? Your prize is a pencil, available at the bookstore.");
}
}

else
{
System.out.println("Congratulations! An exact match!"); //User's guess matches exactly to lottery number
System.out.println("What one word is missing from the following heading? public static ___ main(String[] args)");
String word = keys.nextLine();
}

}

}
Last edited by _inase; 02-15-2011 at 06:20 AM.

2. Senior Member
Join Date
Nov 2009
Posts
235
Rep Power
8
Sorry I can't read your code, Please use the code tags. Also, to answer the question in the title, yes you can have if statements in else if statements. Also, it looks like you are asking if the letter entered = "27" or "40" (the values of c and d). Not if it equals "c" or "d".

3. Member
Join Date
Jan 2011
Posts
13
Rep Power
0
I see what I did wrong. Thanks anyway. I always seem to figure it out after I make a post haha

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•