Results 1 to 9 of 9
Thread: Starsign Program
- 11-10-2011, 04:02 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 27
- Rep Power
- 0
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
- 11-10-2011, 09:28 PM #2
Re: Starsign Program
The logic of your if statements is a little off. For example, the first statement -
How can the day variable be both >= 20 and <= 18?Java Code:if (month >= 1 && day >= 20) { if (month <= 2 && day <= 18) System.out.println("You are a Aquarius"); }
- 11-10-2011, 09:45 PM #3
Member
- Join Date
- Nov 2011
- Location
- Arizona
- Posts
- 10
- Rep Power
- 0
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");
}
- 11-14-2011, 04:41 PM #4
Member
- Join Date
- Nov 2011
- Posts
- 27
- Rep Power
- 0
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
- 11-14-2011, 04:51 PM #5
Re: Starsign Program
What do your if statements look like now, can you post them? javaTurtle's method looks like it should work.
- 11-14-2011, 04:57 PM #6
Member
- Join Date
- Nov 2011
- Posts
- 27
- Rep Power
- 0
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");
}
}
}
- 11-14-2011, 05:02 PM #7
Member
- Join Date
- Nov 2011
- Posts
- 27
- Rep Power
- 0
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
- 11-14-2011, 05:34 PM #8
Member
- Join Date
- Nov 2011
- Location
- Arizona
- Posts
- 10
- Rep Power
- 0
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!
- 11-14-2011, 05:35 PM #9
Re: Starsign Program
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.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?
Last edited by Norm; 11-14-2011 at 05:38 PM.
Similar Threads
-
How to code a program to send messages to a chat program?
By josh2992 in forum New To JavaReplies: 2Last Post: 04-02-2011, 12:57 PM -
How would I open a program from a single button of another program. Help...
By decgaid06 in forum New To JavaReplies: 13Last Post: 03-22-2011, 06:49 AM -
changing my program to array working program
By Chewart in forum New To JavaReplies: 39Last Post: 11-18-2009, 06:53 PM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks