Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

import java.util.Scanner;

public class ZodiacSign{

        public static void main(String args[]){

        Scanner input = new Scanner(System.in);
        int   Feb,March,ZodiacSign,Pisces,Aquarius,Aries,Taurus;

        int selection;
        System.out.println("Feb");

        System.out.println("Feb 1,2,3,4
 Feb 5,6,7,8
 Feb 9,10,11,12
 Feb 13,14,15,16
 Feb 17,18,19,20
 Feb 21,22, 23, 24
 Feb25,26,27,28
 March 1,2,3,4
 March 5,6,7,8
 March 9,10,11,12
 March 13,14,15,16
 March17,18,19,20
 March 21,22,23,24
 March 25,26,27,28
 March 29,30,31");
        System.out.println("Please enter your date of birth");
        System.out.println("Please enter your month of birth");


        selection = input.nextInt();
        if(Feb>= 19){
            ZodiacSign = Pisces ;
            System.out.println("Your zodiac sign is Pisces");
        }   
        else
        {
            System.out.println("Your zodiac sign is Aquarius");
        }
        if(March>=22){
            ZodiacSign = Aries;
            System.out.println("Your zodiac sign is Aries");
        }
        else
        { 
            System.out.println("Your zodiac sign is Taurus");
        }
            System.out.println("End of Program");



        }
}

Its giving me four errors:

ZodiacSign.java:19:error: variable Feb might not have been initialized
                if(Feb>= 19){
                   ^
ZodiacSign.java:20:error: variable Pisces might not have been initialized
                       ZodiacSign= Pisces ;
                                   ^ 
ZodiacSign.java:27:error: variable March might not have been initialized
         if(March>= 22){
            ^
ZodiacSign.java:28:error: variable Aries might not have been initialized
           ZodiacSign= Aries ;
                       ^
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
344 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...