Results 1 to 9 of 9
  1. #1
    WillHorne is offline Member
    Join Date
    Nov 2011
    Posts
    27
    Rep Power
    0

    Default Starsign Program

    Hi

    im trying to write a program that tells you what starsign your in using if and else if statements

    it works for one if statment like this

    import java.util.Scanner;


    public class Starsign {

    /**
    * @param args
    */
    public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    System.out.println("Enter Birth Day");
    int num = scan.nextInt();
    System.out.println("Enter Birth Month");
    int month = scan.nextInt();


    {
    if (month >= 4 && num >= 20){
    if (month <= 5 && num <= 20); }
    System.out.println("You are a Taurus");
    }

    }
    }


    but as soon as i start adding other statements in it dosent work

    import java.util.Scanner;


    public class Month {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner scan = new Scanner(System.in);
    System.out.println("Enter Birth Day ");
    int day = scan.nextInt();
    System.out.println("Enter Birth Month ");
    int month = scan.nextInt();


    {
    if (month >= 1 && day >= 20) {
    if (month <= 2 && day <= 18)
    System.out.println("You are a Aquarius");
    }
    else if (month >= 2 && day >= 19) {
    if (month <= 3 && day <= 20)
    System.out.println("You are a Picies");
    }
    else if (month >= 3 && day >= 21) {
    if (month <= 4 && day <= 19)
    System.out.println("You are a Aries");
    }
    else if (month >= 4 && day >= 20) {
    if (month <= 5 && day <= 20)
    System.out.println("You are a Taurus");
    }
    else if (month >= 5 && day >= 21) {
    if (month <= 6 && day <= 20)
    System.out.println("You are a Gemini");
    }
    else if (month >= 6 && day >= 21) {
    if (month <= 7 && day <= 22)
    System.out.println("You are a Cancer");
    }
    else if (month >= 7 && day >=23) {
    if (month <= 8 && day <= 22)
    System.out.println("You are a Leo");
    }
    else if (month >= 8 && day >= 23) {
    if (month <= 9 && day <= 22)
    System.out.println("You are a Virgo");
    }
    else if (month >= 9 && day >= 23) {
    if (month <= 10 && day <= 22)
    System.out.println("You are a Libra");
    }
    else if (month >= 10 && day >= 23) {
    if (month <= 11 && day <= 21)
    System.out.println("You are a Scorpio");
    }
    else if (month >= 11 && day >= 22) {
    if (month <= 12 && day <= 21)
    System.out.println("You are a Sagittarius");
    }
    else if (month >= 12 && day >= 22) {
    if (month <= 1 && day <= 19)
    System.out.println("You are a Capricorn");
    }
    else
    System.out.print("Invalid Coding");
    }

    }
    }

    Any help would be apreciated

    Thanks

  2. #2
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default Re: Starsign Program

    The logic of your if statements is a little off. For example, the first statement -

    Java Code:
    if (month >= 1 && day >= 20) {
      if (month <= 2 && day <= 18)
        System.out.println("You are a Aquarius");
    }
    How can the day variable be both >= 20 and <= 18?

  3. #3
    javaTurtle is offline Member
    Join Date
    Nov 2011
    Location
    Arizona
    Posts
    10
    Rep Power
    0

    Default Re: Starsign Program

    Hello, try using this as you if statements:

    if ((month == 1 && day >= 20) || (month == 2 && day <= 18))
    {
    System.out.println("You are a Aquarius");

    }

  4. #4
    WillHorne is offline Member
    Join Date
    Nov 2011
    Posts
    27
    Rep Power
    0

    Default Re: Starsign Program

    Hi

    Thanks for the advice but it hasn't worked

    both my version and yours only gives the right output when i input the dates that are in the if statement for each star sign

    e.g entering
    Day -21
    Month -3
    prints out you are an Aries
    but
    Day-22
    Month-3
    prints out nothing


    That's the same for all the signs

  5. #5
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default Re: Starsign Program

    What do your if statements look like now, can you post them? javaTurtle's method looks like it should work.

  6. #6
    WillHorne is offline Member
    Join Date
    Nov 2011
    Posts
    27
    Rep Power
    0

    Default Re: Starsign Program

    import java.util.Scanner;


    public class Month {

    /**
    * @param args
    */
    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner scan = new Scanner(System.in);
    System.out.println("Enter Birth Day ");
    int day = scan.nextInt();
    System.out.println("Enter Birth Month ");
    int month = scan.nextInt();

    {
    if (month == 1 && day >= 20)
    if (month == 2 && day <= 18)
    System.out.println("You are a Aquarius ");

    else if (month == 2 && day >= 19)
    if (month == 3 && day <= 20)
    System.out.println("You are a Picies");

    else if (month == 3 && day >= 21)
    if (month == 4 && day <= 21)
    System.out.println("You are a Aries");

    else if (month == 4 && day >= 20)
    if (month == 5 && day <= 20)
    System.out.println("You are a Taurus");

    else if (month == 5 && day >= 21)
    if (month == 6 && day <= 20)
    System.out.println("You are a Gemini");

    else if (month == 6 && day >= 21)
    if (month == 7 && day <= 22)
    System.out.println("You are a Cancer");

    else if (month == 7 && day >=23)
    if (month == 8 && day <= 22)
    System.out.println("You are a Leo");

    else if (month == 8 && day >= 23)
    if (month == 9 && day <= 22)
    System.out.println("You are a Virgo");

    else if (month == 9 && day >= 23)
    if (month == 10 && day <= 22)
    System.out.println("You are a Libra");

    else if (month == 10 && day >= 23)
    if (month == 11 && day <= 21)
    System.out.println("You are a Scorpio");

    else if (month == 11 && day >= 22)
    if (month == 12 && day <= 21)
    System.out.println("You are a Sagittarius");

    else if (month == 12 && day >= 22)
    if (month == 1 && day <= 19)
    System.out.println("You are a Capricorn");


    else
    System.out.print("Invalid Coding");
    }

    }
    }

  7. #7
    WillHorne is offline Member
    Join Date
    Nov 2011
    Posts
    27
    Rep Power
    0

    Default Re: Starsign Program

    Java Code:
    import java.util.Scanner;
    
    
    public class Month {
    
    	/**
    	 * @param args
    	 */
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    
    		Scanner scan = new Scanner(System.in);
    		System.out.println("Enter Birth Day ");
    		int day = scan.nextInt();
    		System.out.println("Enter Birth Month ");
    		int month = scan.nextInt();
    		
    	{
    		if (month == 1 && day >= 20) 
    			if (month == 2 && day <= 18)
    			System.out.println("You are a Aquarius ");
    			
    		else if (month == 2 && day >= 19) 
    			if (month == 3 && day <= 20)
    			System.out.println("You are a Picies");
    			
    		else if (month == 3 && day >= 21) 
    			if (month == 4 && day <= 21)
    			System.out.println("You are a Aries");
    			
    		else if (month == 4 && day >= 20) 
    			if (month == 5 && day <= 20)
    			System.out.println("You are a Taurus");
    			
    		else if (month == 5 && day >= 21) 
    			if (month == 6 && day <= 20)
    			System.out.println("You are a Gemini");
    			
    		else if (month == 6 && day >= 21) 
    			if (month == 7 && day <= 22)
    			System.out.println("You are a Cancer");
    			
    		else if (month == 7 && day >=23) 
    			if (month == 8 && day <= 22)
    			System.out.println("You are a Leo");
    			
    		else if (month == 8 && day >= 23) 
    			if (month == 9 && day <= 22) 
    			System.out.println("You are a Virgo");
    			
    		else if (month == 9 && day >= 23) 
    			if (month == 10 && day <= 22)
    			System.out.println("You are a Libra");
    			
    		else if (month == 10 && day >= 23) 
    			if (month == 11 && day <= 21)
    			System.out.println("You are a Scorpio");
    			
    		else if (month == 11 && day >= 22) 
    			if (month == 12 && day <= 21)
    			System.out.println("You are a Sagittarius");
    			
    		else if (month == 12 && day >= 22) 
    			if (month == 1 && day <= 19)
    			System.out.println("You are a Capricorn");
    			
    		
    		else 
    			System.out.print("Invalid Coding");
    	}
    		
    }
    }
    Last edited by Norm; 11-14-2011 at 05:34 PM. Reason: added code tags

  8. #8
    javaTurtle is offline Member
    Join Date
    Nov 2011
    Location
    Arizona
    Posts
    10
    Rep Power
    0

    Default Re: Starsign Program

    If its not working, maybe you are still "nesting" the if statements. There is not any need to nest the if statemnets. Post your code so we can see what is going on. Thanks!

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,318
    Rep Power
    25

    Default Re: Starsign Program

    Java Code:
    	if (month == 1 && day >= 20)       // If this is true what is the value for month?
    			if (month == 2 && day <= 18)   // if the above is true, will this ever be true?
    A suggestion: Always use {}s with if statements. Some day you are going to insert a statement following an if and push the following statement out of the if statements control but the indentation will fool you to not see the change in the logic flow.
    Last edited by Norm; 11-14-2011 at 05:38 PM.

Similar Threads

  1. Replies: 2
    Last Post: 04-02-2011, 12:57 PM
  2. Replies: 13
    Last Post: 03-22-2011, 06:49 AM
  3. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 06:53 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 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
  •