Results 1 to 3 of 3
  1. #1
    _inase is offline Member
    Join Date
    Jan 2011
    Posts
    13
    Rep Power
    0

    Default 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. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    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. #3
    _inase is offline Member
    Join Date
    Jan 2011
    Posts
    13
    Rep Power
    0

    Default

    I see what I did wrong. Thanks anyway. I always seem to figure it out after I make a post haha

Similar Threads

  1. need help in sql statement
    By chyeeqi in forum JDBC
    Replies: 6
    Last Post: 03-28-2010, 08:49 PM
  2. for statement help
    By helpisontheway in forum New To Java
    Replies: 5
    Last Post: 11-14-2009, 05:14 PM
  3. if statement help please!!
    By soc86 in forum New To Java
    Replies: 5
    Last Post: 12-02-2008, 03:56 PM
  4. If statement, please help??
    By soc86 in forum New To Java
    Replies: 5
    Last Post: 11-23-2008, 03:58 PM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 05:45 PM

Posting Permissions

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