import java.util.Scanner;
public class ZodiacSign{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int F
Change the following line
int Feb,March,ZodiacSign,Pisces,Aquarius,Aries,Taurus;
to
int Feb=0,March=0,ZodiacSign=0,Pisces=0,Aquarius=0,Aries=0,Taurus=0;
Method variables need some initial assignment before they are used. So do the same with all the other method variables.
You're reading variables which have never been written to, for example you read from Feb
in the first if
statement without ever having written anything to it.
Also, some of the code paths through your code leave ZodiacSign
local variable uninitialized, which will produce the same error once you try to read it. Take for example this:
if(Feb>= 19){
ZodiacSign = Pisces ;
System.out.println("Your zodiac sign is Pisces");
}
else
{
System.out.println("Your zodiac sign is Aquarius");
}
If Feb
is smaller than 19 ZodiacSign
will not have been written by the end of the code fragment. The second conditional also may leave ZodiacSign
unassigned.
What you're missing is actually using the value you read in from the input to set your local variables. That is, after
selection = input.nextInt();
you should use the value in selection
to assign some useful values to Feb
, March
and so on. Then also make sure that ZodiacSign
is assigned no matter which code path ends up being taken.
Moreover, there is a fundamental problem with your algorithm here. You do not need a variable for each month. You need just two variables for your input: day and month and a third for the zodiac sign.
You're never assigning a value to Feb
, so what do you expect this comparison to achieve?
if (Feb >= 19)
Think about what you really wanted to achieve with that comparison, and then work out what you need to change to make it happen.
In general, you can't read from a local variable before it's definitely assigned - in other words, until the compiler can prove that you'll have been through some execution path which assigns it a value.
However, rather than trying to just make it compile with values assigned at the point of declaration, I would suggest you have a closer think about your overall design. You probably want to change Pisces, Aries etc to be enum values for example.
Additionally, Java code usually uses pascalCase
names for local variables.